Hi,
What you are having is a classic getchar() problem for interactive use.
try to run the following example code
#include <iostream>
int main(void) {
std::cout << "test getchar()" << std::endl;
getchar();
std::cout << "test cin" << std::endl;
char c;
std::cin >c;
std::cout << "test getchar() again" << std::endl;
getchar();
return 0;
}
The first time you type 'a' when it pauses followed by ENTER
Then when it pauses type 'b' again followed by enter.
You will notice that the second getchar() command is not pausing.
[]$ ./a.out
test getchar()
a
test cin
b
test getchar() again
[]$
Run it again and type at the first time when it pauses, abc followed by
ENTER. The two next pauses will fail.
[]$ ./a.out
test getchar()
abc
test cin
test getchar() again
[]$
So the problem is the following.
Normally, getchar() is implemented in such a way that it buffers input
until ENTER is pressed. This is called line-buffered input. You have
to press ENTER before anything you typed is actually sent to the
program. Also since getchar() inputs only one character each time it is
called, line-buffering may leave one or more characters waiting in the
input queue, which is annoying in interactive environments like you want
here. Even though Standard C/C++ specifies that getchar() can be
implemented as an interactive function, it seldom is. Therefore it does
not work.
So in the above first run we had the following.
[]$ ./a.out
test getchar()
a << ENTER > // buffer contains "a,ENTER"
// getchar() reads 'a', buffer contains "ENTER"
test cin // cin ignores ENTER and waits for real input,
// buffer empty
b << ENTER > // buffer contains "b,ENTER"
// cin reads 'b', buffer contains "ENTER"
test getchar() again
// getchar() sees ENTER in buffer and uses it, no
// pause!
[]$
In the second test the first buffer contains "a,b,c,ENTE R". getchar()
reads a, cin reads b and still the buffer contains "c,ENTER", hence
getchar() takes the 'c' and the programs exits without any pause.
The problem you have is that you call first cin and then getchar() with
the "ENTER" left in the buffer. so what you need to do is empty your
buffer. This can be done in the following way in your code
int mainMenu() {
char c[10]; // CHANGE THIS
do {
cout << "Main menu" << endl << endl;
cout << "(L)ist" << endl
<< "(A)dd" << endl
<< "(R)emove" << endl << endl
<< "(Q)uit" << endl << endl
<< "Selection: ";
cin >c; // READS THE TYPED WORDS BUT LEAVES
// ENTER IN BUFFER,
// hope you don't type more then 10 chars
getchar(); // REMOVES THE ENTER
switch(c[0]) { // ALTER THIS
case 'l': ; // Fallthrough to uppercase L
case 'L': list(); break;
case 'a': add(); break;
case 'r': remove(); break;
case 'q': break;
}
} while(c[0]!='q'); // ALTER THIS
return 0;
}
When you did this, the use of getchar() in your list function will work,
but only with the use of the "ENTER" keystroke.
If you press at that time any other key and then followed by ENTER your
buffer is full again, and you will have a problem with the next cin in
mainmenu.
The best would be again to change that getchar() in your list function by
char c[10];
std::cin >c;
getchar();
If you did this, your program will work as expected ... at least i think :p
Enjoy and regards
ps take a look at
http://www.parashift.com/c++-faq-lit...html#faq-15.17