这个循环怎么没跳出去

rtrede 2009-01-21 01:55:28

int main()
{
int ref;
while(cin>>ref,!cin.eof())
{
if(cin.bad())
{
throw runtime_error("sfsfsfsdf");
}
if(cin.fail())
{
cerr<<"bad data,input again!";
cin.clear(istream.failbit);
continue;
}
}
return 0;
}


这个循环怎么没跳出去??
...全文
157 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
qqxiaoyatou 2009-01-21
  • 打赏
  • 举报
回复
upup[Quote=引用 9 楼 waizqfor 的回复:]
用cin.sync()请空流 然后continue
[/Quote]
xiaoyisnail 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用楼主 OOOpenHeart 的帖子:]
C/C++ codeintmain()
{intref;while(cin>>ref,!cin.eof())
{if(cin.bad())
{throwruntime_error("sfsfsfsdf");
}if(cin.fail())
{
cerr<<"bad data,input again!";
cin.clear(istream.failbit);continue;
}
}return0;
}

这个循环怎么没跳出去??
[/Quote]

这段代码是c++ primer上的一个错误
1. cin.clear(istream.failbit)并不是清除failbit位,而是设置failbit
2. 需要cin.sync()清空缓冲区
3. 这不是该代码的错误,只是在vc下,while中的!cin.eof()不会退出循环,gcc下可以
nullah 2009-01-21
  • 打赏
  • 举报
回复
cin.sync();清除了缓冲区
所以才没有继续读字符
rtrede 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 nullah 的回复:]
引用 7 楼 OOOpenHeart 的回复:
引用 4 楼 hhyttppd 的回复:
cin.clear();
cin.ignore();

你调试下会发现,还是有点问题

什么问题?
[/Quote]
其实也不叫什么问题,按你的操作,如果我一次输入2个非法字符,他就会出来2个体是,N个非法就N个提示。
用cin.sync();就没这个问题
nullah 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 OOOpenHeart 的回复:]
引用 4 楼 hhyttppd 的回复:
cin.clear();
cin.ignore();

你调试下会发现,还是有点问题
[/Quote]
什么问题?
waizqfor 2009-01-21
  • 打赏
  • 举报
回复
cin.clear(); //重置cin但是不清除内容
waizqfor 2009-01-21
  • 打赏
  • 举报
回复
用cin.sync()请空流 然后continue
rtrede 2009-01-21
  • 打赏
  • 举报
回复
int main()
{
int ref11;
while(cin>>ref11,!cin.eof())
{
if(cin.bad())
{
throw runtime_error("sfsfsfsdf");
}
if(cin.fail())
{
cerr<<"bad data,input again!";
cin.clear(istream.failbit); //////这边不是clear输入流了吗?
cin.clear(); //这为何?
cin.sync(); //这又是?
continue;
}
}
return 0;
}
rtrede 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hhyttppd 的回复:]
cin.clear();
cin.ignore();
[/Quote]
你调试下会发现,还是有点问题
rtrede 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dreamsdark 的回复:]
C/C++ codeintmain()
{intref;while(cin>>ref,!cin.eof())
{if(cin.bad())
{throwruntime_error("sfsfsfsdf");
}if(cin.fail())
{
cerr<<"bad data,input again!";
cin.clear(istream::failbit);
cin.clear();
cin.sync();
continue;
}
}return0;
}
[/Quote]
谢谢
nullah 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hhyttppd 的回复:]
cin.clear();
cin.ignore();
[/Quote]
up
hhyttppd 2009-01-21
  • 打赏
  • 举报
回复
cin.clear();
cin.ignore();
xiaoyisnail 2009-01-21
  • 打赏
  • 举报
回复
gcc下ctrl-z是可以退出循环的
vc下不行
dreamsdark 2009-01-21
  • 打赏
  • 举报
回复

int main()
{
int ref;
while(cin>>ref,!cin.eof())
{
if(cin.bad())
{
throw runtime_error("sfsfsfsdf");
}
if(cin.fail())
{
cerr<<"bad data,input again!";
cin.clear(istream::failbit);
cin.clear();
cin.sync();

continue;
}
}
return 0;
}
xiaoyisnail 2009-01-21
  • 打赏
  • 举报
回复
from msdn:
void clear(
iostate _State=goodbit,
bool _Reraise = false
);
void clear(
io_state _State
);

_State (optional)
The flags you want to set after clearing all flags.
_Reraise
Specifies whether the exception should be re-raised.

64,662

社区成员

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

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