关于iostream异常的问题
有这么一段程序:
void ShowMenu()
{
int choice = -1;
cout < < "Input your choice: " < < endl;
cin > > choice;
switch(choice)
{...}
cin.clear();
cin > > choice;
...
}
如果在cin > > choice时,用户出现了错误输入(如输入了一个字符串等),iostream会做出一定的标记,但加入了cin.clear()之后,为什么第二次cin < < choice操作还是不能正常使用?
[解决办法]
简单点:虽然出错状态被恢复了,可错误数据仍然留在流内没清掉。
要详细,请找《标准C++输入输出流与本地化》
[解决办法]
basic_istream& ignore(
streamsize _Count = 1,
int_type _Delim = traits_type::eof( )
);
#include <limits>
cin.ignore( numeric_limits <streamsize> ::max(), '\n ' ); // 清空输入流 cin.get();