#include <iostream>std::istream& get(std::istream& in) { int ival; while(in>> ival,!in.eof()){ if(in.bad()) throw std::runtime_error( "IO stream corrupted "); if(in.fail()){ std::cerr << "bad data,try again "; in.clear(); continue; } std::cout << ival << "(loop)"; } in.clear(); return in;}using namespace std; int main() {double dval;get(cin); //cin >> dval; //cout << dval << "(outloop)" <<endl; return 0; }問題:我輸入int型的數(shù)是正常的,我輸入非法的數(shù)后,它會一直循環(huán)bad data,try again只能按ctrl+c結束。為什么會循環(huán)呢,不是只打印一次錯誤信息,然后continue就接著輸下一嗎?還有最后兩句就是加了//的那兩句,好像沒什么用?(網(wǎng)上下的例子)
- 2 回答
- 0 關注
- 822 瀏覽
添加回答
舉報
0/150
提交
取消