3

运行以下代码并输入数字时,它工作正常。但是当输入字母时,程序进入死循环,显示“输入数字(0退出):cin失败”。

我的意图是处理 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);
4

3 回答 3

5

除了清除流状态(这是cin.clear所做的)之外,您还需要使用cin.ignore清除cin中的行。

我有几个实用函数可以让这更容易(你会特别对clearline感兴趣,它会清除流状态和当前行),并且几乎是你想要的一个确切的例子。

您的代码或多或少使用我的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(参见示例会话中的“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是一个 typedefstd::basic_istream<char, std::char_traits<char> >
  • std::wistream是一个 typedefstd::basic_istream<wchar_t, std::char_traits<wchar_t> >
  • 扩大允许'\n'变得L'\n'适当
  • 此代码适用于常见的charwchar_t情况,但也适用于basic_istream的任何兼容实例化
  • 它被称为clearline(stream) or ,与 std::endlstd::wsstd::boolalphastream >> clearline等其他操纵器相比
于 2010-01-09T07:42:09.023 回答
1

这可能是你打算做的:

#include <iostream>
using namespace std;

int main ()
{
  int i;
  do {
    if (cin.fail())
    {
      cin.ignore(255);
      cin.clear();
    }          
    cout << "Please enter an integer value: ";
    cin >> i;
  } while ( cin.fail() );
  cout << "The value you entered is " << i;
  return 0;
}
于 2010-01-09T07:44:05.977 回答
0

这是 cin.fail() 的简单示例,它将处理输入,直到提供有效的整数值

#include <iostream>
using namespace std;

int main()
{
int j;
int i;

i = 0;
while (1) {
    i++;
    cin >> j;
    if (cin.fail()) return 0;
    cout << "Integer " << i << ": " << j << endl;
}
}

输入:

42 51 85 你好 85

输出:

整数 1:42

整数 2:51

整数 3:85

于 2015-10-07T16:03:23.263 回答