Page 1 of 1

Real quick simple question

Posted: Mon Mar 23, 2009 9:45 pm
by clc02
Say you have this
cin >> char a;
cin >> char b;
If you typed in 12 it wouldn't stop at the second cin. This can get annoying because if someone typed 123 it might not only bypass that but go back to somewhere else and enter in a value you didn't want in the first place.
Is there a way to empty out cin? (Meh, I don't know how to phrase it)

Re: Real quick simple question

Posted: Mon Mar 23, 2009 9:51 pm
by eatcomics
I'm lost, anyone else know....

Re: Real quick simple question

Posted: Mon Mar 23, 2009 10:23 pm
by avansc
just build a data validation functions that make sure the user enters the right thing.

Re: Real quick simple question

Posted: Mon Mar 23, 2009 10:29 pm
by clc02
I did that. I have a tic tac toe function. It make sure it's 1 2 or 3, but, when it returns to the previous function it has a option to exit out. It's 2. So if a person did enter 222 (2, 2 is the position) the 2 will carry over to the previous function and enter it to exit the program.

Re: Real quick simple question

Posted: Tue Mar 24, 2009 9:07 am
by MarauderIIC
Is there a way to empty out cin?
http://www.cplusplus.com/reference/iostream/istream/sync.html wrote:int sync ( );

Synchronize input buffer with source of characters

Synchronizes the buffer associated with the stream to its controlled input sequence. This effectively means that the unread characters in the buffer are discarded.

Code: Select all

char a;
cin >> a;
cin.sync();
Would work, I assume.

Or a not as good way to do it

Code: Select all

#include <string>
using namespace std;

string dummy;
getline(cin, dummy);
?

Re: Real quick simple question

Posted: Tue Mar 24, 2009 11:21 am
by MadPumpkin
Mar's, "Not as good way to do it" is the way i use for everything, so that one looks good to me lol :)