Calculator program input errors

Hello, before I ask my question must say you guys are amazing and thank you for answering my question. The problem is that the else if statement outputting the error line is after the line that asks what the second number is, so if you enter something that is not a valid operation, the program will say enter the second number and then print the error line. I want the program to run smoothly and enter the error line before the second number and so the program will keep running.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````//Beginner calculator program #include using namespace std; int main() { cout << "Welcome to the calculator program " << endl; //Classic program intro cout << "Enter the first number: " << endl; double num1; cin >> num1; //prompting user for 1st num cout << "Enter the operation you would like me to perform (+, -, *, /): " << endl; char operation; cin >> operation; //prompting user for operation cout << "Enter the second number: " << endl; double num2; cin >> num2; //prompting user for second number if(operation =='+') { double z; z = num1 + num2; cout << num1 << " + " << num2 << " = " << z << endl; cout << "The sum is " << z << endl; } //making the addition statement if(operation == '-') { double z; z = num1 - num2; cout << num1 << " - " << num2 << " = " << z << endl; cout << "The difference is " << z << endl; } //making the subtraction statement if(operation == '*') { double z; z = num1 - num2; cout << num1 << " * " << num2 << " = " << z << endl; cout << "The product is " << z << endl; } //making the multiplication statement if(operation == '/') { double z; z = num1 - num2; cout << num1 << " / " << num2 << " = " << z << endl; cout << "The quotient is " << z << endl; } //making the division statement else if(operation) { cout << "Error, invalid operation " << endl; } }``````
Last edited on
If you would use code tags when you post, it would be much easier to point where in the code to make changes.

After `cin>>operation;` put
 ``1234`` ``````if ((operation!='+')||(operation!='-')||(operation!='*')||(operation!='/')) { //output here a message and exit program }``````

Then you don't need the else if. By the way, if you select '+' for example, your program executes that option, then it checks if the operator is '-' (which is useless at this point, since you selected +), then does the same thing with *. When it tries to do that with '/', it does not satisfy the condition, so it will go to the else statement, and say that you have an invalid operation. You should use else if before -,*,/. Like I said, you would like to deal with other cases before the second number, so you don't need the last else

A nice solution would be for you to use a while loop around the input for the operation, so you repeat that until a valid operator is entered
Wait
Last edited on
So what exactly does the if statement do?

 ``1234`` ``````if ((operation!='+')||(operation!='-')||(operation!='*')||(operation!='/')) { cout << "invalid operation " << endl; }``````

it doesn't care if it is valid or invalid and doesn't exit the program so I'm stumped...
It would be easier for you to use a switch statement or loop that will loop through different inputs and execute a block of code depending if the input is a valid input, else it will do a default task. Also I kind of made your code a little bit easier to read.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````//Beginner calculator program #include using namespace std; int main() { //Variable Initialization int operation = 0; //Int that will determine the opration to perform double num1; //User input 1 double num2; // User input 2 double ans; //var to hold the ans and display. //Classic program intro cout << "Welcome to the calculator program " << endl; //prompting user for 1st num cout << "Enter the first number: " << endl; cin >> num1; //prompting user for operation cout << "Enter the number that corresponds to the operation you want me to do: " << endl; cout << "(0) + " << endl; cout << "(1) - " << endl; cout << "(2) * " << endl; cout << "(3) /" << endl; cin >> operation; //prompting user for second number cout << "Enter the second number: " << endl; cin >> num2; //Goes through the loop to check what the user inputed. //if one of the cases is true then the block under the case is executed, else the default block is evecuted. switch(operation) { case 0: ans = num1 + num2; cout << ans << endl; break; case 1: ans = num1 - num2; cout << ans << endl; break; case 2: ans = num1 * num2; cout << ans << endl; break; case 3: ans = num1 - num2; cout << ans << endl; break; //If none of the cases are true then default is executed. default: cout << "Invalid Operator Input, Only and intger 0 - 5!! \n"; break; } }``````

Sorry, my mistake for the condition. It should be

 ``1234`` ``````if !((operation=='+')||(operation=='-')||(operation=='*')||(operation=='/')) { //output here a message and exit program }``````

If operator is any of +-*/, one of the conditions is true, so everything in parentheses is true, and I negate that, so the condition is not executed. If operator is not one of those 4, no condition is true, the compound condition is false, and by negation I run whatever is in between the curly brackets. Make sure you exit the program (you can return -1 for example), so as not to ask for number 2.
Thank you very much for the solution and the easier to read code Mr. Hertz, one question,

in this line of code

 ``1234`` ``````//If none of the cases are true then default is executed. default: cout << "Invalid Operator Input, Only and intger 0 - 5!! \n"; break;``````

what exactly is happening?
with a default statement if the value does not meet any of the previous conditions then it executes default instead.
Topic archived. No new replies allowed.