I was looking at this code and wanted to what is the reason for lines 35 - 39:
" if(!cin)
{
cin.clear();
cin.ignore(100, '\n');
}
"
I'm not quite sure what this is being used for. Can anyone explain what this is actually doing? Thanks!
It checks if anything has been inputted and not actually dumped into a character. This is usually done for when you... well, do things like character or number input and switch types. Otherwise, you can end up with it automatically fetching a response from the buffer and not letting you type.
In addition to what ispil said it checks if an error state have been set on the stream . There error States can be set
1. failbit - it is set when what is described by ispil happens entering a
char for an integer etc.
2. eofbit - set when the stream strikes an end of file (eof) character.
3. badbit - is set when the stream runs into unrecoverable error such as corrupted data.
You can clear the first and second errors by using cin.clear () and can continue using the stream .