【问题标题】:loop or code for user wrong input until correct input c++用户错误输入的循环或代码,直到正确输入c ++
【发布时间】:2021-07-16 07:52:34
【问题描述】:

我是 C++ 新手,我被分配创建一个计算器并要求用户输入并计算数字。我还必须创建一个代码,当用户输入错误的数字时,程序会要求用户输入正确的数字。当用户输入错误的输入时,如何创建代码,它会再次重复整个代码。

#include <iostream>
using namespace std;

int UI() {
  int choice;
  cout << "Enter the correspendonce number for the following options you want "
          ": \n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n\n "
          "Option: ";
  cin >> choice;
  return choice;
}

int add(int a, int b) {
  int c = a + b;
  return c;
}

int subtract(int a, int b) {
  int c = a - b;
  return c;
}

int multiply(int a, int b) {
  int c = a * b;
  return c;
}

int divide(int a, int b) {
  int c = a / b;
  return c;
}

// main function
int main() {
  int total;
  int num1, num2;
  int choice;

  choice = UI();

  if (choice == 1) {
    cout << "Input two numbers you wish to calculate for :";
    cin >> num1;
    cin >> num2;
    total = add(num1, num2);
    cout << " the total is " << total << "\n";
  } else if (choice == 2) {
    cout << "Input two numbers you wish to calculate for :";
    cin >> num1;
    cin >> num2;
    total = subtract(num1, num2);
    cout << " the total is " << total << "\n";
  } else if (choice == 3) {
    cout << "Input two numbers you wish to calculate for :";
    cin >> num1;
    cin >> num2;
    total = multiply(num1, num2);
    cout << " the total is " << total << "\n";
  } else if (choice == 4) {
    cout << "Input two numbers you wish to calculate for :";
    cin >> num1;
    cin >> num2;
    total = divide(num1, num2);
    cout << " the total is " << total << "\n";
  } else {
    cout << "Entered wrong input, please select option 1 - 5";
  }
  return 0;
}

【问题讨论】:

    标签: c++ calculator


    【解决方案1】:

    一个非常简单的方法是设置一个标志:

    bool done = false;
    while (!done)
    {
        done = true;
        int choice = UI();
    
        if (choice == 1) {
        
            // etc ...
    
        } else {
            cout << "Entered wrong input, please select option 1 - 4\n";
            done = false;
        }
    }
    

    另一种选择是将逻辑卸载到您的输入函数中:

    // Gets 1-based choice up to count.  Returns 0 on error.
    int GetChoice(int count)
    {
        while (cin >> choice)
        {
            if (choice >= 1 && choice <= count) {
                return choice;
            }
            cout << "Invalid choice.  Enter value from 1 to " << count << endl;
        }
        return 0;
    }
    

    那么你可以简单地调用:

    int choice = GetChoice(4);
    

    问题是如果用户输入hello 或任何非整数,您将收到错误消息。使用基于行的输入解决此问题的最简单方法:

    // Note, you also need to include <string> and <sstream>
    
    int GetChoice(int count)
    {
        string input;
        while (getline(cin, input))
        {
            int choice;
            istringstream iss(input);
            if (input >> choice) {
                if (choice >= 1 && choice <= count) {
                    return choice;
                }
            }
            cout << "Invalid choice.  Enter value from 1 to " << count << endl;
        }
        return 0;
    }
    

    这些是一些实用的解决方案。如果你愿意,你可以设计一些更漂亮的东西。

    【讨论】:

    • 非常感谢,这真的很有帮助,我的代码运行顺利!
    【解决方案2】:

    你可以使用 goto 作为基本的,所以当用户输入除 1、2、3 或 4 之外的任何数字时,她会要求她再次输入一个数字。但是请记住,在良好的编码中不建议使用 goto练习。它为您的简单问题提供了一个简单的解决方案。

     int UI() {
                int choice;
            x:   
                cout << "Enter the correspendonce number for the following options you want "
                    ": \n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n\n "
                    "Option: ";
               
                cin >> choice;
                switch (choice)
                {
                case 1:
                case 2:
                case 3:
                case 4:
                    return choice;
                default:
                    cout << "Please enter 1&2&3 or 4\n";
                    goto x;
                }
            }
    

    【讨论】:

    • 来吧,这不是 1989 年......使用goto 解决这个问题是应受谴责的建议。
    • @paddy 我绝对同意你的观点,使用 goto 是不好的,我在回复中说明了这一点。如果我们看这个问题,问这个问题的朋友是非常基本的。我的目标是为他提供一个简单的解决方案,而不会造成语法人群和难以理解。否则使用 goto 当然不好。
    • 欢迎您以这种方式证明这一点,但请记住,您已经通过使用 switch 引入了新语法并推荐了一个根本不常见的解决方案。就像有人遇到你的“简单解决方案”一样,杀死他们而不是谈判。这可能是一个解决方案,但绝不是常态,只是更多问题的开始。
    • @paddy 感谢您分享您的宝贵经验。从现在开始,我会在帮助他人的同时考虑您所说的话。
    【解决方案3】:

    用以下代码替换您的代码。 简单的解决方案是将要重复的代码包装在 while 循环中 并且循环只会在用户输入正确的输入时结束,否则循环会重复,结束循环使用“break”。

    int main() {
      int total;
      int num1, num2;
      int choice;
    
    while(1)
    {
      choice = UI();
    
      if (choice == 1) {
        cout << "Input two numbers you wish to calculate for :";
        cin >> num1;
        cin >> num2;
        total = add(num1, num2);
        cout << " the total is " << total << "\n";
        break;
      } else if (choice == 2) {
        cout << "Input two numbers you wish to calculate for :";
        cin >> num1;
        cin >> num2;
        total = subtract(num1, num2);
        cout << " the total is " << total << "\n";
        break;
      } else if (choice == 3) {
        cout << "Input two numbers you wish to calculate for :";
        cin >> num1;
        cin >> num2;
        total = multiply(num1, num2);
        cout << " the total is " << total << "\n";
        break;
      } else if (choice == 4) {
        cout << "Input two numbers you wish to calculate for :";
        cin >> num1;
        cin >> num2;
        total = divide(num1, num2);
        cout << " the total is " << total << "\n";
        break;
      } else {
        cout << "Entered wrong input, please select option 1 - 5";
      }
    }
      return 0;
    }
    

    【讨论】:

    • 你做了什么改变,为什么?一个好的答案应该解释事情并帮助人们更好地理解语言。仅仅提供代码就会导致货物崇拜编程。
    • @churill 我明白你在说什么我编辑了我的答案。
    • 这引入了潜在的错误,因为代码重复 - 对代码的任何更改都必须认识到 break 对于防止错误行为至关重要(例如如果添加新的选择)。虽然在这样一个微不足道的示例中识别和测试这一点很简单,但要始终牢记这一点,因为有一天您将在一个更加复杂的环境中编写代码,并且使行为尽可能明显是有益的。
    猜你喜欢
    • 2017-02-15
    • 2021-11-19
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多