【发布时间】:2017-07-12 16:08:10
【问题描述】:
我现在正在学习 C++ Primer 第 4 版并玩 IO 流。当我尝试运行书中的代码时(第289页):
#include <iostream>
#include <string>
using namespace std;
int main()
{
int ival;
// read cin and test only for EOF; loop is executed even if there are
// other IO failures
while (cin >> ival, !cin.eof()){
if (cin.bad()) // input stream is corrupted; bail out
throw runtime_error("IO stream corrupted");
if (cin.fail()){
cerr << "bad data, try again\n";
cin.clear(istream::failbit);
continue;
}
//ok to process ival
}
}
我在输入一个字符串(比如“as”)后遇到了一个无限循环。那么这段代码有什么问题呢?
【问题讨论】:
-
你说的是“C++ Primer 4th edition”。请提供作者姓名(最好是 ISBN)来消除歧义