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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>

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
1
2
3
4
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");

1
2
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:

1
2
3
4
5
6
7
8
9
10
11
 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.