What sort of behavior are you getting? What does the code that's before it do? Your solution to this situation lies in those details. Unfortunately, we don't have them so all that we can do is speculate at best.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
What happened to Narue's old pinned thread on clearing the buffer? It was great.
I won't recreate it here. The issue is that >> and get and getline act differently, so when you mix them, you often end up with whitespace remaining in what you thought was a clean buffer. When in doubt, clear the stdin stream using the ignore command
http://www.cplusplus.com/reference/iostream/istream/ignore/
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
That's because you are using the "extraction operator" (>>) for numeric input. When you do this, the newline that the ENTER key puts on the input stream does not get removed from the stream. To correct this, you must place a cin.ignore() ( info about istream::ignore() ) after each numeric input with that operator.
...
int someNum = 0;
...
cout << "Please enter a number: ";
cin >> someNum;
cin.ignore();
...
EDIT:
@VD:>>What happened to Narue's old pinned thread on clearing the buffer? It was great.
Agreed, I wish I knew too...
>>When in doubt, clear the stdout stream using the ignore command
Definitely, but it's actually the stdin stream that is the problem...
EDIT 2:
Found Narue's thread: http://www.daniweb.com/forums/thread90228.html
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
>>When in doubt, clear the stdout stream using the ignore command[/B]
Definitely, but it's actually the stdin stream that is the problem...
Edited it after posting and before seeing your post. Scout's Honor. :) Good catch.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
As far as I know, there is no input operation in Standard C/C++ that behaves that way. You'll probably have to use an o/s-specific API call for that. But I don't know any o/s APIs just yet.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
You don't. The C++ standard does not allow for "hit any key" type input. You need to change your prompt to "Press ENTER to continue"
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Yes. But why end all cases with the same 3 statements? Put them once at the bottom of the loop after the switch structure.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944