问题
After a failbit is set: When I first call cin.clear() and then cin.ignore(), the programe is right. And when I first call cin.ignore() and then cin.clear(), the ignore seems to not work, why?
回答1:
cin.clear()
clears the failbit, but cin.ignore()
doesn't.
it means that, if the stream is in an invalid state, calling clear()
followed by ignore()
will reset the state to good, then ignore the next character.
On the other hand, calling ignore()
followed by clear()
means ignore()
will fail, then clear()
will proceed to reset the stream state. So, in that case, the next character will not be ignored.
回答2:
cin.ignore()
only does its work of consuming data from the stream if cin.good()
is true
. If the failbit
is set, then cin.good()
will be false
.
来源:https://stackoverflow.com/questions/12002747/does-the-failbit-effect-the-call-ignore-on-cin