Help with calculator

everything compiles. my question is that when a,b is 0 by using equation / or %, it tries to solve now i tried everything i can think off to stop from solving. if any one has any input would be greatly 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
56
57
58
59
60
61
62
63
64
65
66
67
68
  //Francis Palagano 
//10/09/2015
//Algorithm 
//1 ask user to enter a simple A "+,-,*,/, or % "  B equation.
//2 calculate equation
//    1 if problem contains +, calculate a+b= result
//    2 if problem contains -,  calculate a-b = result 
//    3 if problem contains /,  calculate a/b 
//        1 if a = 0, tell user divisor cannot be 0
//    4 if problem contains  *, calculate a*b = result
//    5 if problem contains  %, calculate a%b = result
//        1 if a = 0, tell user divisor cannot be 0
//    5 if problem does not contain stated above display invalid operation
//3 display results as a(operation)b=result
//4 end program

#include <iostream>
using namespace std;

int main()
{
   int a, b, result;
   char operation;
 
   cout << " Please enter a simple mathmatical equation using +,-,*,/, or % : " ;
   cin >> a >> operation >> b ;
   
   switch(operation)
   {
   case '+':
         result = a + b ;
         break;

   case '-':
         result = a - b ;
         break;

   case '*':
         result = a * b;
         break;

   case '/': 
        if ( a,b == 0 ) 
        { cout << "Divisor cannot be 0" << endl;}
        else result = a / b;
        break;
         
   case '%':
        if ( a,b == 0 )
        { cout << "Divisor cannot be 0"  << endl; }
        else result = a % b;
        break; 
       

   default:
         cout << "Invalid operation." << endl;
         return -1;
}

   
   cout << " The answer to your equation " << a << operation << b << "=" <<  result << endl;


   
   system("pause");
   
   return 0;
}
Here's what I would do for this.
1
2
3
4
5
6
7
8
9
case '/':
     if(b == 0) // not a,b
     {
          std::cout << "Divisor can't be 0" << std::endl;
          return -1;
     }
     else
          result = a / b;
     break;

Returning -1 here ends the program just like it does in you default case.
thank you very much
Topic archived. No new replies allowed.