The error message "Wrong input. Please try again." is displayed when the user enters an invalid operator.
C++
int performOperation(double numberOne, double numberTwo, std::string operation) {
double result;
if (operation == "+") {
result = numberOne + numberTwo;
} else
if (operation == "-") {
result = numberOne - numberTwo;
} else
if (operation == "*") {
result = numberOne * numberTwo;
} else
if (operation == "/") {
result = numberOne / numberTwo;
} else { // Handle invalid inputs
cout << "Invalid input. Please enter a valid operator (+, -, *, /): ";
cin >> operation;
return performOperation(numberOne, numberTwo, operation); // Recursive call to re-evaluate with corrected input
}
return result;
}
So, in the above modified code, if an invalid operator is entered, the program prompts the user to enter a valid operator. The performOperation() function is called again with the corrected input.
So, This ensures that the user can enter a valid operator until the arithmetic operation can be performed correctly.