Real quick simple question

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
clc02
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Sun Mar 22, 2009 9:55 pm

Real quick simple question

Post 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)
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Real quick simple question

Post by eatcomics »

I'm lost, anyone else know....
Image
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Real quick simple question

Post by avansc »

just build a data validation functions that make sure the user enters the right thing.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
clc02
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Sun Mar 22, 2009 9:55 pm

Re: Real quick simple question

Post 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.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Real quick simple question

Post 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);
?
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: Real quick simple question

Post 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 :)
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
Post Reply