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

Viewed 583

I am new to c++, and I was assigned to create a calculator and ask for the user input and calculate the numbers. I also have to create a code for when the user inputs the wrong number, the program will ask the user to input the right number. How do I create a code for when the user enters the wrong input, it repeats the whole code again.

#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;
}
3 Answers

A very simple approach is to set a flag:

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

Another option is to offload that logic into your input function:

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

Then you can simply call:

int choice = GetChoice(4);

The problem with this is you'll get an error if the user enters hello or any non-integer. The simplest way to get around this using line-based input:

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

These are some practical solutions. You could engineer something fancier if you wish.

You can use goto as basic, so when the user enters any number other than 1, 2, 3 or 4, she will ask her to enter a number again.But remember, the use of goto is not recommended in good coding practice.It offers a simple solution to your simple problem.

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

Replace your code with the below code. Easy solution is that you wrap the code which you want to repeat in while loop and the loop will only end when the user enters correct input otherwise loop will repeat, to end the loop use "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;
}
Related