C++primer(4版)的状态流的查询与控制

xiexiufeng 2007-01-26 04:14:25
在看<<C++primer>>(4版时),测试了书上的一个例子,如下:
int main()
{
int ival;
while(cin >> ival, !cin.eof())
{
if(cin.bad())
throw runtime_error("IO stream corrupted");
if(cin.fail())
{
cerr << "bad data, try again" << endl;;
cin.clear(istream::failbit); //reset the stream
continue; //get next input
}
}
return 0;
}
输入一组整数肯定时没问题了,可输入,比如:10 20 test 30;死循环,不断输出:
bad data, try again
程序的本意是通过while()循环不段读入cin,直到到达文件末尾或者发生不可恢复的读取错去为止.字符串test赋给int型ival导致istream::failbit为真,因此需重置流的状态,而continue书上说是get next input,可是通过显示bad data, try again,说明流并没有读取下一个值,还是读取的那个错值,即test!

不懂,why?谢谢大家能看看!
...全文
509 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiexiufeng 2007-01-30
  • 打赏
  • 举报
回复
真诚的谢谢楼上各位的讲解!

C++primer 中讲:s.clear(flag) 将流s中的某个指定状态设置为有效状态.flag的类型为str::iostate,而s.clear() 则是将s中的所有状态都重新设为有效状态!因此我认为参数flag是
设置前的错误的流状态,其实是错误的,s.clear(flag)是想要重新设置成的流的状态!

对于taodm(taodm)指教,我试了,cin.ignore(10000, '\n')是忽略直到换行前的所有字符,比如我输入:10 20 30 test 40 50,那么程序就会只显示:10 20 30,而忽略了40 50,我也将'\n'改为了'\40'(空格符),但还是不行,是因为cin.clear(flag)我就理解错了,将流置成failbit()状态了!

对于todototry()给的例子,我很感谢,不过我觉得有个地方改下就完美了,ignore()默认的参数是忽略一个字符,如果输入的是一个单词test,就会显示很多的:bad data, try again".改为ignore(1000, "\40")一般就可以忽略空格前的所有字符了!

To :chinesealbert(chinesealbert)
我是买的书,我是学生,有学生证打7.5折,呵呵~~

chinesealbert 2007-01-30
  • 打赏
  • 举报
回复
第四版在哪有下载的?
有中文吗?
chinesealbert 2007-01-30
  • 打赏
  • 举报
回复
mark
todototry 2007-01-29
  • 打赏
  • 举报
回复
clear函数执行两步
1。清楚现在的状态
2。设置新的状态(默认goodbit)
sdlyczl 2007-01-29
  • 打赏
  • 举报
回复
basic_ios::clear
void clear(iostate state = goodbit);
The member function replaces the stored stream state information with state | (rdbuf() != 0 ? goodbit : badbit). If state & exceptions() is nonzero, it then throws an object of class failure.
哪位老大解释一下这段话什么意思?看不懂啊
sdlyczl 2007-01-29
  • 打赏
  • 举报
回复
原来是:
cin.clear(istream::failbit); //reset the stream  改成
cin.clear(ios_base::goodbit); //reset the stream
taodm 2007-01-29
  • 打赏
  • 举报
回复
cin.clear();
cin.ignore(10000, '\n');
todototry 2007-01-29
  • 打赏
  • 举报
回复
int main()
{
int ival;
while(cin >> ival, !cin.eof())
{
if(cin.bad())
{
throw runtime_error("IO stream corrupted");
cin.clear(ios_base::goodbit);
}
if(cin.fail())
{
cerr << "bad data, try again" << endl;;
cin.clear(ios_base::goodbit); //reset the stream
cin.ignore();
continue; //get next input
}
}
return 0;
}
sdlyczl 2007-01-29
  • 打赏
  • 举报
回复
to:Top
taodm(taodm) ( ) 信誉:100 Blog
cin.clear(istream::failbit); //reset the stream
cin.ingore(10000, '\n');---->改成ignore
========================================
我试了一下,还是不行
htqx 2007-01-28
  • 打赏
  • 举报
回复
2分题.
taodm 2007-01-28
  • 打赏
  • 举报
回复
你试了没有?
xiexiufeng 2007-01-27
  • 打赏
  • 举报
回复
能解决吗,好像不能吧!偶对cin的读入机制不是很理解,希望能给出具体的解决方案,不胜感激!
taodm 2007-01-26
  • 打赏
  • 举报
回复
cin.clear(istream::failbit); //reset the stream
cin.ingore(10000, '\n');

65,195

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧