I know that getline() returns either eofbit, failbit, or badbit. My question is how do I find out what is returned. I can't find anything about this on Google/Bing Search.
You have to clear the stream (to reset the error bits)
Thank you, used cin.clear(ios::goodbit);
cin.getline() doesn't clear the stream like getline() does so as firedraco said, you need to clear it yourself.
How do you use getline() alone? I get an error if I remove the cin.
The function is cin.fail() rather than cin.failbit(). Or you could do !cin.good().
Ahh, well that helps! Much shorter then cin.rdstate()==ios::failbit
Don't want to use !cin.good() because I don't want to display for the user to enter a shorter line if it is a fatal error, but good to know!
Overall, here is the full while statement. Plus the first cin.getline()
1 2 3 4 5 6 7
cin.getline(var.line1,50);
while(cin.fail()){
cout<<"Error: Your line is too long.\nPlease enter a shorter line: ";
cin.clear(ios::goodbit);
cin.ignore(99,'\n');
cin.getline(var.line1,50);
}
Note: For future people reading this, you can also use cin.clear(cin.good()), which is 2 characters shorter. :)