If you look on the main C++ page in Daniweb you will see a posting "How do I flush the input stream".
This should help
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
Between lines 11 and 12 put
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
The stream >> operator only reads what you ask it to, and leaves everything else behind in the input buffer. However, whenever dealing with user input you must expect that the user will press the ENTER key after everything you ask him to enter.
getline() reads everything except the ENTER key, stores it in a string, then reads and throws away the ENTER key.
cin >> anything, in contrast, only reads anything, and leaves everything else (including that pesky ENTER key) in the input stream. So the next time you call getline(), it does what it did before, reading '' and sticking it in to a string, then reads the ENTER key and throws it away.
So, after using >> you must explicitly read and throw away that ENTER key so that it isn't still there the next time you try to get input from the user.
Hope this helps.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
yes that's right and to correct the problem use
std::cin.ignore(1);
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
When you read in the character into answer, you are actually reading in two characters. The first character is 'y', the second character is '\n'. The 'y' is grabbed and stuck into the variable answer. That leaves the '\n' still in that buffer. Next comes the getline command. Let's pretend nothing was in the buffer. You have this command:
getline (cin, studentName);
If there was nothing in the buffer and the user just hit the return key, that's a perfectly legal entry. The empty string ("") would be placed in the studentName.
Now with the newline character ('\n') already in the buffer, when the computer hits that command, it treats the newline character the same as if it was typed in AFTER the getline command was issued. It makes no distinction between whether that Enter key was typed before or after the user is prompted by the getline command. Hence an empty string is placed into studentName and the computer doesn't pause for user input since it thinks it already HAS enough input to do its job and so doesn't wait for any more. That's why the input stream needs to be flushed out before the getline command.
So yes, I think you have a correct understanding.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
I think you should ignore more than 1 character. That'd be enough if the person actually does only enter one character, which is what they are supposed to. But what if they get ahead of themselves, like we all do, and forget that they have to just type in one character, and they type in "Jonathon" or something? In other words, go straight for the name entry by accident. The buffer takes in "Jonathon", plus the '\n', stores 'J' in the character variable answer, throws away 1 character, which is the first 'o' in "Jonathon", then you write an error message to type 'y' or 'n', "nathon" is left in the buffer, the variable answer grabs the first letter 'n', the computer thinks that means the user is done, and exits, when it was just a mistake. I say think of some number like 256 that's bigger than any possible name, and throw that many characters away, which'll clear the entire input stream.
http://www.cplusplus.com/reference/iostream/istream/ignore.html
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
It is not working because you failed to obey my advice.
Read up on istream::ignore() to find out why it fails.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
Keep in mind that we have run across many professors who teach quite the opposite of good practice.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
And secondly, if you are going to ask for advice you can't ignore the advice given you and complain things still don't work. I didn't walk into your house and tell you to sit. I answered your question.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229