【问题标题】:how to allow multiple inputs in case of an incorrect data type entry in cpp?如果 cpp 中的数据类型输入不正确,如何允许多个输入?
【发布时间】:2016-02-15 06:25:34
【问题描述】:

我有一个程序可以生成随机数并要求用户继续猜测,直到他/她猜对为止。即使我通过处理错误情况错误地输入了任何其他数据类型,我也希望它继续接受新值。

我的问题是,当我尝试运行以下程序时,只要我输入一个字符并按 Enter,它就会进入无限循环。我尝试使用 cin.ignore() 和 cin.clear() 但这只会使程序在第一次进入后停止。

谁能帮助我了解发生了什么以及如何实现所需的输出?提前致谢。

#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int main()
{
  int secret_num, guess;
  srand(time(NULL));
  secret_num=rand() %  101 + 0;
  cout<<"Enter your guess between 0 and 100: ";

do
 {
  if(!(cin>>guess))
  {
    cout<<" The entered value is not an integer"<<endl;
  }
  else if( isnumber(guess))
    {
      if(guess>secret_num)
        cout<<"Too high";
      else if(guess<secret_num)
        cout<<"too low";
    cout<<endl;
    }
 }
  while(secret_num!=guess);


  if((guess==secret_num)| (isnumber(guess)))
  {
    cout<<"yes the correct number is "<<secret_num<<endl;
  }

  return 0;
}

编辑:这是我的代码中使用 cin.clear() 和 cin.ignore(1000,'\n') 时输出的屏幕截图,当我在输入字符两次后输入数字时。

【问题讨论】:

  • 什么是isnumber()

标签: c++


【解决方案1】:
    if (!(cin >> guess))
    {           
        cout << " The entered value is not an integer" << endl;
        cin.clear(); // clear must go before ignore

        // Otherwise ignore will fail (because the stream is still in a bad state)
        cin.ignore(std::numeric_limits<int>::max(), '\n'); 
    }

默认情况下 cin.ignore 将忽略单个字符。如果他们输入超过 1 个字符,那就不够了,所以我稍微修改了一下。

if ((guess == secret_num) | (isnumber(guess)))

| 是位运算符 [OR]

|| 是逻辑运算符 [OR]

但我认为你真正想要的是&amp;&amp; (AND)

if ((guess == secret_num) && (isnumber(guess)))

【讨论】:

  • 不,它仍然无法正常工作。我曾尝试使用相同但使用 1000 而不是限制。如果我继续输入字符,它工作正常。但是一旦我输入一个数字,它就会停止做任何事情。
【解决方案2】:

有几个问题。

  1. 您应该按照@José 的建议使用cin.clear()cin.ignore()

  2. isnumber() 是什么?我猜它正在返回false,所以没有打印出提示消息(即“太高”和“太低”),看起来它停止了,虽然它只是在等待下一个输入。 isnumber() 对我来说没有意义。 guess 已被声明为 int,它必须是一个数字,不是吗?

  3. if((guess==secret_num)| (isnumber(guess))) 在这里是不必要的。直到用户输入正确的数字,循环才会结束,这个条件应该已经成立了。

【讨论】:

  • 非常感谢您指出。当我编写代码时,我用 isumeric() 编译了我的代码。编译器显示一个错误,说我应该使用 isnumber() 代替。我没有费心交叉检查它,因此出现了这个错误。我完全删除了 isnumber(),现在程序运行良好。
【解决方案3】:

你可以使用clear和flush

  if(!(cin>>guess))
  {
    cout<<" The entered value is not an integer"<<endl;
    cin.clear();
    fflush(stdin);
  }

如果您从控制台读取,则此方法有效。否则,您可以选择@José 的答案。

【讨论】:

    【解决方案4】:

    我会更改循环内的逻辑,因为有一些无用的测试。这对我有用:

    #include <iostream>
    #include <limits>
    #include <cstdlib> // You may take a look at <random> and <chrono>
    #include <time.h>
    
    using std::cout;
    using std::cin;
    
    int main() {
    
        srand(time(NULL));
        int secret_num = rand() %  101;
        cout << secret_num << '\n';
        cout << "Enter your guess between 0 and 100:\n";
    
        int guess = -1;
        do {
            cin >> guess;
            if ( cin.eof() ) 
                break;
            if ( cin.fail() ) {
                cout << "The entered value is not an integer, please retry.\n";
                // clear the error flag
                cin.clear();
                // ignore the rest of the line
                cin.ignore(std::numeric_limits<int>::max(),'\n');
                // clear the value of the variable
                guess = -1;
                continue;
            }
            // now we know that guess is a number
            if ( guess > secret_num )
                cout << "Too high\n";
            else if ( guess < secret_num )
                cout << "Too low\n";
            else {
                cout << "Yes the correct number is " << secret_num << std::endl;
                break;
            }
        } while ( true );
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-07
      • 1970-01-01
      • 2013-05-22
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多