【问题标题】:Verifying User Input as Integer: User has to enter input twice验证用户输入为整数:用户必须输入两次
【发布时间】:2019-05-22 11:09:42
【问题描述】:

我正在提示用户输入一个整数值。当值不正确时,程序运行。但是,当用户输入整数输入时,用户需要输入两次。

我查看了有关如何使用 while 循环捕获错误输入的其他教程,这部分对我有用。但是,整数值需要输入两次才能运行程序。

#include <iostream>
using namespace std;

int main() {
    cout << "*************************************************" << endl;
    cout << "******************|DVD Library|******************" << endl;
    cout << "*************************************************" << endl;
    cout << "1.\tAdd DVD" << endl;
    cout << "2.\tDelete DVD" << endl;
    cout << "3.\tSearch DVD" << endl;
    cout << "4.\tList All DVDs in the Library" << endl;
    cout << "5.\tAdd DVD to Favorites List" << endl;
    cout << "6.\tDelete DVD from Favorites List" << endl;
    cout << "7.\tSearch DVD in Favorites List" << endl;
    cout << "8.\tList All DVDs in Favorites List" << endl;
    cout << "9.\tQuit" << endl;
    cout << "*************************************************" << endl;

    int input;
    cin >> input;
    while (!(cin >> input)) {
        cin.clear();
        while (cin.get() != '\n')
            continue;
        cout << "Please enter an integer --> " << flush;
    }
    if (input < 1 || input > 9) {
        cout << "Invalid input! Please try again!" << endl;
    }
    return 0;
}

【问题讨论】:

  • 。 o O(DVD 是如此的 90 年代)

标签: c++


【解决方案1】:

你要求输入两次:

cin >> input;
while(!(cin >> input )){

删除第一行可能会使其按预期工作。

【讨论】:

    【解决方案2】:

    '用户必须输入两次'看看你的代码

    int input;
    cin >> input;
    while(!(cin >> input )){
    

    你要求用户输入多少次?

    你会更幸运的

    int input;
    while(!(cin >> input )){
    

    您的错误恢复代码看起来很合理,但尚未测试。

    【讨论】:

      【解决方案3】:
      int input;
      while (cout << "Your choice: ",
             !(cin >> input) || input < 1 || 9 < input)
      {
          cin.clear();
          while (cin.get() != '\n');
          cerr << "Invalid input! Please try again!\n";
      }
      

      【讨论】:

        【解决方案4】:

        谢谢大家! “cin >> 输入;”线是不必要的。起初,我把它留在那里,因为如果用户输入了一个数字输入,例如双精度数,它实际上会告诉用户错误消息。因此,如果用户输入类似 3.3 的内容,程序将显示我指定的错误消息(“请输入整数”行)。但是,在这种情况下(当有双精度时)程序要求用户提示输入整数两次,然后继续程序。当我删除上述不必要的行时,程序接受双重输入,但它的作用是取小数点前的数值并将其用作整数。所以,1.2的值在我测试的时候记录为1。我不确定为什么会发生这种现象,但程序可以正常工作。也许是人为错误的原因?

        #include <iostream>
        using namespace std;
        
        int main() {
            cout << "*************************************************" << endl;
            cout << "******************|DVD Library|******************" << endl;
            cout << "*************************************************" << endl;
            cout << "1.\tAdd DVD" << endl;
            cout << "2.\tDelete DVD" << endl;
            cout << "3.\tSearch DVD" << endl;
            cout << "4.\tList All DVDs in the Library" << endl;
            cout << "5.\tAdd DVD to Favorites List" << endl;
            cout << "6.\tDelete DVD from Favorites List" << endl;
            cout << "7.\tSearch DVD in Favorites List" << endl;
            cout << "8.\tList All DVDs in Favorites List" << endl;
            cout << "9.\tQuit" << endl;
            cout << "*************************************************" << endl;
        
            int input;
            while (!(cin >> input)) {
                cin.clear();
                while (cin.get() != '\n')
                    continue;
                cout << "Please enter an integer --> " << flush;
            }
            if (input < 1 || input > 9) {
            cout << "Invalid input! Please try again!" << endl;
        }
        return 0;
        

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-20
          • 2020-07-22
          • 1970-01-01
          • 2011-07-20
          • 2017-09-11
          相关资源
          最近更新 更多