Clearing the istream

I don't like using ststem("PAUSE"); to pause my program, so instead I use something along the lines of cin.get('\n'), and tell the user to press enter.

The problem is, if there was somethingpreviously in the istream, it carries over, including the new line character. So my program ends withoput prompting.

Is there a way I can clear my istream so that nothing is carried over into what I input next?
I know there is a cin.clear() function, but I am not sure what patameters it takes, or if just sounds like what I would want.

Thanks in advance.
Last edited on
Yes that will clear the stream. Here is a reference to it: http://www.cplusplus.com/reference/iostream/ios/clear/

Btw, cin.get() uses '\n' by default; you do not need to specify it.
No, it won't do what you think.

cin.clear(); resets the state flags for the stream. It does not remove any input.


The trick when dealing with all your input is to remember this:

Whenever the user is asked for information, he will always press ENTER when done.

Therefore, whenever your program gets information from the user, it needs to get rid of those ENTER key presses lurking in the input.

Further reading:
http://www.cplusplus.com/forum/beginner/1988/page2.html#msg9770
http://www.cplusplus.com/forum/beginner/2408/#msg9277
http://www.cplusplus.com/forum/beginner/1988/page4.html#msg14423

http://www.cplusplus.com/forum/articles/6046/

Hope this helps.
I stand corrected. I misunderstood the " The value existing before the call has no effect. " I guess.
Topic archived. No new replies allowed.