0

I have a bug in a simple calculator program. C++

I can't figure out what is going on in this condition of my while loop. I'm trying to say in my condition; while the variable op does not equal to char +, -, /, or *, then continue through the if statements. //This is a calculator app that adds, subtracts, multiplies, or divides two numbers. #include <iostream> using namespace std; int main() { //Sets up variables. num1 and num2 are inputs by user, result is the calculation of the two. Op is a char that the user inputs, either -, +, /, or *. double num1; double num2; double result; char op; //Sets up the instructions and inputs all information from user. cout << "Welcome to my calculator." << endl; cout << "Enter your first number: "; cin >> num1; cout << "The operator can be a; +, -, /, or *" << endl; cout << "Enter the operator: "; cin >> op; cout << "Enter the second number: "; cin >> num2; //Calculates the inputs by the user or informs them that there was an error. ----->while (op != '+' || '-' || '/' || '*') {<--------------------------------------------------------- cout << "The operator can be a; +, -, /, or *" << endl; cout << "Enter the operator: "; cin >> op; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '/') { result = num1 / num2; } else if (op == '*') { result = num1 * num2; } else { cout << "Invalid operator." << endl; } } //Prints the calculation on the screen and ends the function. cout << "And the total is:" << endl; cout << result; return 0; }

5th May 2019, 2:09 PM
Ignatious
2 odpowiedzi
+ 2
the way you are comparing variable to multiple values in while loop is wrong and you need to run while loop when all of your condition is wrong so use 'and' operator instead of 'or' while (op != '+' && op!='-' &&op!= '/' && op!= '*') { cout << "The operator can be a; +, -, /, or *" << endl; cout << "Enter the operator: "; cin >> op; } if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '/') { result = num1 / num2; } else if (op == '*') { result = num1 * num2; }
5th May 2019, 2:45 PM
kiRA
kiRA - avatar
0
Thank you.
6th May 2019, 2:32 AM
Ignatious