用户错误输入的循环或代码,直到正确输入 C++

loop or code for user wrong input until correct input c++

我是 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;
}

你可以使用goto作为basic,所以当用户输入除1、2、3或4以外的任何数字时,她会要求她输入一个数字again.But记住,goto的使用不是在良好的编码中推荐 practice.It 为您的简单问题提供了简单的解决方案。

 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;
            }
        }

将您的代码替换为以下代码。 简单的解决方案是将要重复的代码包装在 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;
}

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

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;
}

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