While (std::cin num1)
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 :|
Quote:Original post by Lavajumper
1) how do I correct the state of cin after using this line.
Use the istream::clear member function.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement