simple question

So idk why I can't remember this, probably cause I haven't done it in a while. But why is it that while(cin >> int) never exits even with blank lines and cin.ignore() in the code? How can I accomplish this? Can't find anything on google as it all relates to strings, not ints.

Thanks for the help

I figured this would work, but it never exits...
1
2
3
4
while (cin >> int) {
     //do something with int
     cin.ignore();
}
Last edited on
cin >> something gobbles up all leading whitespace (this includes newlines).

It should work if you enter something that's not an integer.

It should also work if you redirect cin to a file (cin will eventually hit an invalid character or the end of the file, whichever comes first), or if you throw in an EOF character (signal?) yourself (it's Ctrl+Z on a Windows cmd.exe shell; not sure about other OSes).

What exactly are you trying to accomplish?

If you're trying to loop while the user still wants to give you input, keep in mind that cin doesn't have the power to magically read the user's mind, tell when the user is done inputting things, and then automagically fail itself on the next input, so you're going to have to set up a specific input (or type of input) that the user has to enter in order to break the loop.
Topic archived. No new replies allowed.