Does the failbit effect the call ignore on cin?

ぐ巨炮叔叔 提交于 2019-12-11 01:00:25

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!