Deleting extraneus input

If I am prompting the user for two chars, in order, for different parts of the code, and the user inputs two characters the first time, the next character stores into the second char.

For example:
//segment
char a, b='y';
cin>>a;
if (a=='n')
cout>>"this is n";
while(b!='f')
{cout<<"type the letter f";
cin>>b;}
//end segment

If the user is prompted for a, and types nf, the pregram will ask for b, but the input is already there: it is f, so the user never actually inputs b.

This is useful in some instances, but for others, like the one above, it can be annoying, especially if there is a lot of code between the two cins.

Is there a way to make the program not care about the input after the iniitial character?

Thanks in advance.
Last edited on
Yes, use the cin.ignore member function after getting the input for a.
http://cplusplus.com/reference/iostream/istream/ignore/
http://cplusplus.com/forum/beginner/9148/
Last edited on
I am not quite sure how to use this, as all of the examples have been discussing other methods of input, and i have only gone over the >> operator.

Could you give me a quick summary of otherwise how to do it?
After you read in a, use cin.ignore() to ignore the rest of the input. Then when you go to read input for b, there will be no input left over.
Topic archived. No new replies allowed.