【问题标题】:How to clear line of input, not just single character如何清除输入行,而不仅仅是单个字符
【发布时间】:2019-06-28 17:32:32
【问题描述】:

对不起,如果这是一个简单的问题,我是初学者。如果不是预期的类型,我希望能够从 cin 清除输入。我让它适用于单个字符或值,但是当我在行上输入多个字符时会出现问题。

例如,提示用户输入双精度数。如果它不是双重的,我会收到一条错误消息并重新提示。如果我输入更长的字符串,也会出现这种情况。

EX 1:预期输出

Enter initial estimate: a

The initial estimate is not a number.
Enter initial estimate: afdf

The initial estimate is not a number. 

EX 2:在我目前的代码中,afdf 被不断读取,所以我得到:

Enter initial estimate of root : a

The initial estimate was not a number
Enter initial estimate of root : afdf

The initial estimate was not a number
Enter initial estimate of root :
The initial estimate was not a number
Enter initial estimate of root :
The initial estimate was not a number
Enter increment for estimate of root :
The increment was not a number

我尝试过使用 cin.clear() 和 cin.get() 以及查看 getline() 但这不起作用。

 while (numTries < 4)
 {
   numTries++;
   cout << "Enter initial estimate of root : ";
   cin >> estimate;

   if (!(cin.fail()))
   {
     if ((estimate >= minEst) && (estimate <= maxEst))
     {
       break;
     }
     else
     {
       if (numTries == 4)
       {
         cout << "ERROR: Exceeded max number of tries entering data" << endl;
         return 0;
       }
       cout << "" << endl;
       cout << "Value you entered was not in range\n";
       cout << fixed << setprecision(3) << minEst << " <= initial estimate <= " << maxEst << endl;
     }
   }
   else
   {
   cout << "\nThe initial estimate was not a number\n";
   cin.clear();
   cin.get();
   }
 }

如何确保在下次输入时清除输入?我可以使用 getline() 来实现这一点吗?提前致谢。

【问题讨论】:

    标签: c++ get cin getline


    【解决方案1】:

    您可以将输入检索为字符串并对其进行解析以检查它是否为数字:

    bool convert_string_to_double(const std::string &str, double &out_value){
        try{
            out_value = stod(str);
            return true;
        } catch (const std::invalid_argument &e) {
            return false;
        }
    }
    
    bool get_double_from_input(double &out_value){
        std::string input_str;
    
        cin >> input_str;
    
        return convert_string_to_double(input_str, out_value);
    }
    

    然后使用get_double_from_input 从输入中检索双精度值。如果将 value 转换为 double 失败,它将返回 false,或者返回 true 并将结果存储到 out_value

    【讨论】:

      【解决方案2】:

      如果您想坚持使用 cin,那么您将需要忽略 cin.ignore() 的其余部分

      #include<limit>
      ...
      
      double estimate;
      do {
          if(cin.fail()) {
              cin.clear();
              cin.ignore(numeric_limits<streamsize>::max(), '\n');
              cout << "The initial estimate was not a number" << endl;
          }
          cout << "Enter initial estimate of root: ";
          cin >> estimate;
          cout << endl;
      } while(!cin);
      

      Getline 可能是更好的选择,因为它从由换行符 (\n) 分隔的输入流中获取一行。

      do {
          if(cin.fail()) {
              cin.clear();
              cout << "The initial estimate was not a number" << endl;
          }
          cout << "Enter initial estimate of root: ";
      } while(!getline(cin, estimate);
      

      【讨论】:

      猜你喜欢
      • 2021-12-31
      • 2019-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多