LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 779|回复: 1

流控制中的一个奇怪问题(cin.clear( ))

[复制链接]
发表于 2007-8-20 19:46:59 | 显示全部楼层 |阅读模式
正在学c++,看c++primer
第八章中的一个事例程序:
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";
                        cin.clear();
                        continue;
                cout<<ival<<endl;
                }
        }
}

./a.out
a
Bad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try againBad data, try

输入非数字字符,程序怎么进入死循环了,奇怪。
哪位能指点一下吗?先谢谢了!
 楼主| 发表于 2007-8-20 21:09:38 | 显示全部楼层
google了一下,找到原因了。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表