Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

How can I flush out the rest of the users input (cin)?

For example I have the following code:

 


char cCommand;
cout << "Enter: ";
cin >> cCommand;
if (cCommand == 'c')
{
... do something ...
cout << "Valid Input";
}
else
{
cout << "Invalid Input";
}
[/Code]

 

Now this works perfectly fine when the user enters a single character, but what if the user enters something like this:

Enter: abc

In this case the user enters "abc" my code will out the following:

Invalid Input

Invalid Input

('a' & 'b' are invalid but 'c' is valid)

 

This is not the behavior I want... Seeing as 'abc' is not 'c' I want to output "Invalid Input" and then FLUSH the rest and force him to retry, I do not want to continue and process the "bc" parts of the input ...

 

Is there a way, after cout << "Invalid Input" to somehow FLUSH the rest of what the user entered so that he is forced to start over, and it does not process the rest of his input?

Thanks,

  • Leaders
Posted
Have you tried reading a string from the stream? This should read an entire line. Then you could process the string char by char, or validate it as a whole.
[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...