For one of my assignments, I was told to use cin.clear(ios_base::failbit) to set the failbit.  I wonder, what is the difference between cin.clear(ios_base::failbit) and cin.setstate(ios_base::failbit)?  Is the latter not clearer?  I am a bit surprised to see how the failbit is set with clear().  Why such a counter-intuitive way for using clear()?
I am a bit surprised to see how the fail bit is set with clear(). Why such a counter-intuitive way for using clear()?
Well let's think about it.
 std::cin.setstate(ios_base::failbit);
This will set the fail bit if it isn't already set, but preserve any other stream error state for std::cin.
 std::cin.clear(ios_base::failbit);
This will clear all the stream error state from std::cin and then set only the fail bit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With