### need basic if statement help

Ok, so I am making a basic number calculator using if statements and I have most of it done, and correctly. I just can't figure out how to add two things: For example if the user inputs an operator that is incorrect or unavailable it will output "error: no such operator" and if the user enters a problem that cannot be answered such as 200/0 then it will output "error: attempted division by zero".

Also, what should I use besides system ("PAUSE") to have the same effect without the security problems?

Here is the code I have so far, any help is much appreciated.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include using namespace std; int main() { int num1; char op; int num2; int answer; cout << "Please enter your first number: " << endl; cin >> num1; cout << "Please enter the operator you wish to use, choices: +,-,/,*: " << endl; cin >> op; cout << "Please enter your second number: " << endl; cin >> num2; if(op == '+'){ answer= num1 + num2; cout << num1 << " + " << num2 << " = " << answer << endl; } if(op == '-'){ answer = num1 - num2; cout << num1 << " - " << num2 << " = " << answer << endl; } if(op == '*'){ answer = num1 * num2; cout << num1 << " * " << num2 << " = " << answer << endl; } if(op == '/'){ answer = num1 / num2; cout << num1 << " / " << num2 << " = " << answer << endl; } system("PAUSE"); return 0; } ``````
Last edited on
For the operators you could simply do something like
 ``1234`` ``````if((op != '+') && (op != '-') && (op != '*') && (op != '/')) { //Output error message }``````

Also, would `_getch();` be a good replacement for system(pause) ?
EDIT: You need to include conio.h to use _getch().
Last edited on
It would be quite difficult to account for all the different scenarios.

basically you just create different if statements for each situation you think you will likely encounter.

http://stackoverflow.com/questions/4747934/c-catch-a-divide-by-zero-error

you can try to use exceptions

http://www.cplusplus.com/doc/tutorial/exceptions/

for example when a user enters a letter and you can ask the user again to enter a numeric value.

i normally use instead of system ("PAUSE");

 ``12`` ``````int exit; cin >> exit; ``````

Thank you guys for the help so far. I have gotten the error message part down for the operator but I am trying to do the error message for "error: you cannot divide by zero" and when I run what I have and try to divide a number by zero the program freezes and I am forced to close it.

Here is that part of my code:

 ``1234567891011`` `````` if(op == '/'){ answer = num1 / num2; cout << num1 << " / " << num2 << " = " << answer << endl; if(num2 == 0) cout << "Error: cannot divide by zero!" << endl; }``````

any ideas??
That's because you're doing the calculation before you check whether you have valid inputs for it. If num2 is 0, then line 3 will cause undefined behaviour, and if your program crashes there, then it will never get to the check on line 6.

There's no point checking your input after you've already performed the calculation. You need to rearrange the code so that:

1) You check for valid input before you actually attempt the calculation
2) You only perform the calculation if the inputs are valid
Wow, that was such an obvious solution!

Thanks MikeyBoy!
No problem :) Sometimes, it's hard to see what's right in front of you!
Topic archived. No new replies allowed.