As an amater programmer, this confuses me. I’m probably just missing something. In C++ code that I’ve seen things like this:
while (cin.get(c)) {...}
almost as though it was a try statement. If the statement succeeds, it’s like the function returned true, if not, it’s like it returned false. I’ve seen things like this a lot, sometimes in other languages. How does it work? I’m I just missing something (like the function returning false if it doesn’t work.)? If not, does this work in all languages?
Take a look here: http://www.cplusplus.com/reference/iostream/istream/get/
The version that you are using is the second (
istream& get ( char& c );). In C++, as pst said, any value different of 0 or NULL will be true, otherwise it will be false. In your case, every call of cin.get will return *this, wich will be true, so, you will get an infinite loop until some “problem” occurs like reading an EOF “char” (^C => Ctrl+C) in Windows).