关于 cin.clear() 的问题
我正在阅读 Accelerated C++ 这本书。 (我的问题位于第57页,如果你们有这本书的话)
问题如下: 我们确实有一个读取学生成绩的函数:
...
while (in >> x) { // in and x are defined as an istream& and as a double
hw.push_back(x); // hw is vector defined as vector<double>&
}
in.clear();
...
现在,在书中以及 cplusplus.com 参考文献中都指出,清除函数会重置所有错误状态,并且输入现在再次准备好读取某些输入。 问题是,如果我
int a = 0;
cin >> a;
cout << a << endl;
在函数后面放一个:,它会跳转 cin 并只给我一个 0。我是否完全错误地理解了 cin.clear() 的功能,或者我该怎么做才能使 cin 再次处于活动状态。
因为我在读这本书之前有一段时间遇到了同样的问题,所以我知道我当时用下面的行解决了这个问题:
cin.ignore( numeric_limits<streamsize>::max(), '\n');
当然,然后我必须按一个额外的回车键,但它会吃掉之前出现的所有内容,以及使 cin 无法工作。
问题是 .clear 和 .ignore 单独工作都不能正常工作,但是将它们一起使用我可以为变量 a 输入一些内容;
编辑:好的,这是整个代码。这是我自己写的,不是书上的。
istream& in = cin;
int var = 0;
vector<int> vec;
while ( in >> var ) {
vec.push_back(var);
}
for (int f = 0; f < vec.size(); ++f) {
cout << vec[f] << endl;
}
cin.clear();
cout << "cleared" << endl;
int a = 0;
cin >> a;
cout << a << endl;
I am just working through the book Accelerated C++. (My problem is located on page 57, if you guys have the book with you)
The problem is the following:
We do have a function which reads student grades:
...
while (in >> x) { // in and x are defined as an istream& and as a double
hw.push_back(x); // hw is vector defined as vector<double>&
}
in.clear();
...
Now, in book and also on the cplusplus.com refernce is stated that the clear function resets all the error states and that the input is now again ready to read some input.
The problem is that if I put a:
int a = 0;
cin >> a;
cout << a << endl;
after the function it jumps the cin and just gives me a 0. Did I understand the function of cin.clear() totally wrong or what can I do to get the cin active again.
As I had the same problem a while before I read the book, I know that I solved the problem back then with the following line:
cin.ignore( numeric_limits<streamsize>::max(), '\n');
Of course I then have to hit an extra enter key but it eats all the stuff which comes before and which makes the cin not to work.
The thing is that neither .clear nor .ignore work properly alone but using them both together I am able to enter something for the variable a;
EDIT: Ok, here is the whole code. This is something I have written myself, which is not from the book.
istream& in = cin;
int var = 0;
vector<int> vec;
while ( in >> var ) {
vec.push_back(var);
}
for (int f = 0; f < vec.size(); ++f) {
cout << vec[f] << endl;
}
cin.clear();
cout << "cleared" << endl;
int a = 0;
cin >> a;
cout << a << endl;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对
clear
的调用会清除读取失败所设置的错误状态。它不会对输入流中可能存在的字符执行任何操作。如果错误状态是由于读取双精度数失败而导致的,则下一个字符作为整数读取时很可能也会失败。
如果你尝试一下,
我相信效果会更好。
否则,您将不得不
忽略
一些字符来消除不可读的输入。The call to
clear
clears the error state set by a failed read. It doesn't do anything with the characters that might be present in the input stream.If the error state is a result of failing to read a double, it is likely that the next character will also fail as an integer.
If you try
I'm sure that will work better.
Otherwise you will have to
ignore
some characters to get rid of the unreadable input.代码示例中的 while 循环读取整数值,直到输入结束或读取时发生错误,例如给出不能作为整数出现的字符,如“q”。
发生错误时,您可能有机会通过调用
clear()
来恢复该错误,然后从cin
的输入缓冲区中删除有问题的字符:然后您可以再次读取
int
。The while loop in your code example reads integer values until the end of input or an error occurs while reading, e.g. giving a character that cannot occur as an integer digit like 'q'.
When an error occured, you might have a chance to recover from that error by calling
clear()
and then removing the offending character fromcin
's input buffer:Then you can read an
int
again.