当运行下面的代码并输入一个数字时,它工作正常。 但是当输入一个字母时,程序进入死循环,显示“输入一个数字(0退出):cin failed。”
我的意图是处理 cin 失败案例并再次提示用户。
int number;
do{
cout << "Enter a number (0 to exit): ";
cin >> number;
if(cin.fail()){
cout << "cin failed." << endl;
cin.clear();
}else{
cout << "cin succeeded, " << number << " entered." << endl;
}
}while(number != 0);
最佳答案
除了清除流状态(这就是 cin.clear 确实)。
我有several utility functions为了使这更容易(您将特别对 clearline 感兴趣,它清除流状态和当前行)并且几乎是 what you want 的精确示例.
你的代码,或多或少,使用了我的clearline:
#include "clinput.hpp" // move my file to a location it can be used from
int main() {
using namespace std;
while (true) {
cout << "Enter a number (0 to exit): ";
int number;
if (cin >> number) {
cout << "Read " << number << '\n';
if (number == 0) {
break;
}
}
else {
if (cin.eof()) { // tested only *after* failed state
cerr << "Input failed due to EOF, exiting.\n";
return 1;
}
cerr << "Input failed, try again.\n";
clearline(cin); // "cin >> clearline" is identical
}
}
return 0;
}
这里仍然存在一个潜在的问题(在我的 clinput_loop.cpp 中用 blankline 修复),将输入留在缓冲区中会搞砸以后的 IO(参见示例 session 中的“42 abc” ).将上述代码提取到一个单独且自包含的函数中作为练习留给读者,但这里有一个框架:
template<class Type, class Ch, class ChTr>
Type read(std::basic_istream<Ch,ChTr>& stream, Ch const* prompt) {
Type value;
// *try input here*
if (could_not_get_input or more_of_line_left) {
throw std::runtime_error("...");
}
return value;
}
template<class Type, class Ch, class ChTr>
void read_into(
Type& value,
std::basic_istream<Ch,ChTr>& stream,
Ch const* prompt
) {
value = read<Type>(stream, prompt);
}
使用示例:
int n;
try {
read_into(n, std::cin, "Enter a number: ");
}
catch (std::runtime_error& e) {
//...
raise;
}
cout << "Read " << n << '\n';
clearline 为后代提取的函数,以防以上链接中断(并稍作更改以使其独立):
#include <istream>
#include <limits>
template<class C, class T>
std::basic_istream<C,T>& clearline(std::basic_istream<C,T>& s) {
s.clear();
s.ignore(std::numeric_limits<std::streamsize>::max(), s.widen('\n'))
return s;
}
如果你不习惯,模板的东西会有点困惑,但并不难:
- std::istream 是
std::basic_istream<char, std::char_traits<char> >
的类型定义 - std::wistream 是
std::basic_istream<wchar_t, std::char_traits<wchar_t> >
的类型定义 - 加宽 允许
'\n'
成为L'\n'
酌情 - 此代码适用于常见的 char 和 wchar_t 情况,也适用于 basic_istream 的任何兼容实例化
- 写成
clearline(stream)
或stream >> clearline
,与 std::endl、std::ws 或 std::boolalpha 等其他操纵器相比
关于C++ cin.fail() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2032719/