std::cin.clear()
clears the fail bit which would be set if something went wrong e.g. you tried to extract an int into a char
char test;
std::cin >> test;
If 2
was entered here then that would set the fail bit. If that is set then std::cin
will no longer attempt to extract characters e.g.
int again;
std::cin >> again;
Would do nothing. Calling std::cin.clear()
puts std::cin
back into a “good” state. However std::cin
is still holding that 2
which be used instead of getting new input.
So if instead of again
you were trying to do the same thing (using the first example with test
) then that would result in the exact same situation.
std::cin.ignore()
extracts a character and discards it.