Sign in to follow this  
Lavajumper

While (std::cin num1)

Recommended Posts

I'm reading C++ Primer 4th ed. and consider the book so far to be brilliant. That is why I'm utterly shocked that its having me use a cin as a while condition without explaining the fact that once you exit the loop by entering a different type, all subsequent uses of cin fail to work as intended. I assume that while (std::cin >> num1) is proper and commonly used as a method to loop input especially in the case of reading from a file. My questions are: 1) how do I correct the state of cin after using this line. 2) The book uses this line frequently and as early as chapter 1. Assuming this cin behavior wasn't overlooked, I ask: does anyone know if the book explains this in a later chapter or can anyone explain why this information was left out? edit: ignore the improper subject code :|

Share this post


Link to post
Share on other sites
Quote:
Original post by Lavajumper
1) how do I correct the state of cin after using this line.


Use the istream::clear member function.


Share this post


Link to post
Share on other sites
I'm a fan of grabbing things as strings (or a full line of text) and then parsing it using std::stringstream, reading into a string can't fail unless you run out of input (or memory ;)! I just find it easier to read and write such code.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this