Jump to content
  • Advertisement
Sign in to follow this  
Lavajumper

While (std::cin num1)

This topic is 4060 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!