I'm a C++ beginner and I'm currently working on a simple calculator project. I want the input to be only numbers so I made a if else statement to check if the input is only number, if yes then do the calculation and return to "Enter operator" bla bla. Please ignore case 2 3 4 as I want to make the input checker work. What am I doing wrong and how do I fix this ? Thank you in advance ! But for some reason it just wouldn't work.
# include <iostream>
using namespace std;
int main() {
bool whileRunning = true;
while(whileRunning){
char op;
float num1, num2;
cout << "\n Enter operator either 1 (+) or 2 (-) or 3 (*) or 4 (/) or 5 to exit: ";
cin >> op;
switch (op)
{
case '1':
cout << "Enter two operands: ";
cin >> num1 >> num2;
if (isdigit(num1) == 0 && isdigit(num2) == 0) {
cout << num1 + num2;
}
else
cout << " Please only input numbers";
break;
break;
case '2':
cout << "Enter two operands: ";
cin >> num1 >> num2;
if (isdigit(num1) && isdigit(num2)) {
cout << num1 - num2;
}
else
break;
break;
case '3':
cout << "Enter two operands: ";
cin >> num1 >> num2;
if (isdigit(num1) && isdigit(num2)) {
cout << num1 * num2;
}
else
break;
break;
case '4':
cout << "Enter two operands: ";
cin >> num1 >> num2;
if (isdigit(num1) && isdigit(num2)) {
if (num2 == 0) {
cout << "You tried to divide by 0, Try again!";
break;
}
else
cout << num1 / num2;
}
else
break;
break;
case '5':
whileRunning = false;
break;
default:
// If the operator is other than +, -, * or /, error message is shown
cout << "Error! Your input must be >= 1 and <= 5. Please try again";
break;
}
}
return 0;
};