Simple Calculator with switch function not working

Hi, I am new to c++, and I have encountered a problem in my calculator program. When I execute it with any number or operational(except divide by 0), it shows "Please enter a valid operation". How to do I make it so that it will be functioning properly?

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
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

double cal(double &x, double &y);
int result;
char op; //The operation
double firstnum;
double secondnum;
int main()
{

cout << "Please insert your first number" << endl;
cin >> firstnum;
cout << "Please insert operator" << endl;
cin >> op;
cout << "Please insert your second number" << endl;
cin >> secondnum;
if (op = '/' && secondnum == 0)
{
   cout << "Attempting to divide by 0? You will blow up the world!!!" << endl;
}
else{
    cal (firstnum, secondnum);
}
return 0;
}

double cal(double &x,double &y)
{
x = firstnum;
y = secondnum;




    switch (op)
        {
        case '+':
            result = x + y;
            cout << "The answer is " << result << endl;
            break;
        case '-':
            result = x - y;
            cout << "The answer is " << result << endl;
            break;
        case '*':
            result = x * y;
            cout << "The answer is " << result << endl;
            break;
        case '/':
            result = x / y;
            cout << "The answer is " << result << endl;
            break;
        default:
            cout << "Please enter a valid operation" << endl;
            break;
        }



}
Your main problem here is on line 21:
if (op = '/' && secondnum == 0)

You forgot to put a second = for op == '/'.

Other than that, your cal function doesn't actually return anything, although you say that it returns a double, and global variables are nasty (don't use them if you can help it).
Thanks, didn't see that.
Topic archived. No new replies allowed.