Advance Calculator

I was messing around with Code Blocks and i want to create a calculator There user choose 2 numbers and then writes +,-,:,x and program excecutes however i got errors and no clue how to fix it.Can anyone tell me is it worth or do i need find a new approach

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

using namespace std;

int main()
{
    int a,b,c,pl,su,di,mu,sum;
    cout << "2 Numbers calculator" << endl << endl;
    cout << "Insert first number" << endl; 
    cin >> a;
    cout << "Insert second number" << endl;
    cin >> b;
    cout << "What do you want to do with the numbers (+,-,x,:)"; //(user inserts sign +,-,x,:)
    cin >> c; // +, -,x,: holder
    pl = a+b; // Plius adds values
    su = a-b; // Minus subtracts values
    mu = a*b; // Multiples Values
    di = a/b; // Divide Values

    if (c = "+"){        
        cout << pl << endl;
    }else if (c = "-"){
        cout << su << endl;   
    }else if (c = "x"){
        cout << mu << endl;
    }else (c = ":"){
        cout << di << endl;
    }

return 0;
}

The problem is that you are using double quotes when you should be using single quotes. Also you should use a character rather an integer for inputting the sign. One more thing to mention you should probably only do one calculation ( the one that is needed ) and use a switch[1] instead of if/else.

Here is a very simple version of what you are doing:

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

#include <iostream>

using std::cout;
using std::endl;
using std::cin;

int main()
{
    int lhs , rhs , result = 0;
    char oper;

    cout << "Please enter an equation [a][sign][b] ex: ( 10 + 2 ):" << endl;
    cin >> lhs >> oper >> rhs;

    switch ( oper )
    {
        case '+':
            result = lhs + rhs;
            break;
        case '-': 
            result = lhs - rhs; 
            break;
        case '*': 
            result = lhs * rhs; 
            break;
        case '/': 
            if( rhs == 0 ) 
                cout << "Error: Can't divide by 0" << endl;
            else 
                result = lhs / rhs; 
            break;
        default: 
            cout << "Invalid operation" << endl;
    }

    cout << lhs << ' ' << oper << ' ' << rhs << " = " << result << endl;

    return ( 0 );
}


[1] http://www.cplusplus.com/doc/tutorial/control/
http://ideone.com/ZKwSVJ
Thank you very much
Topic archived. No new replies allowed.