Calculator, help with input

Hello, im very noob at this. This is a calculator i wrote that kind of works.
The thing i want to change is the 1,2,3,4 input to add,sub,mul,div. I have no idea how to make this work.

I had to steal the idea of 1,2,3,4 from another post because it wouldn't work with add,sub etc..

I have tried "if (answer == add)", does not work.
"add = 1", does not work.

Any tips?

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
  
int main()
{


    int a, b, add, sub, mul, div, answer;
    cout << "Enter your first value: ";
    cin >> a;
    cout << "Enter your second value: ";
    cin >> b;

    cout << "Type 1 for additon, 2 for subtraction, 3 for multiplication and 4 for division: ";
    cin >> answer;

    add = a + b;
    sub = a - b;
    mul = a * b;
    div = a / b;



    if (answer == 1)
    {
       cout << "Result: " << add << endl;
    }

    else if (answer == 2)
    {
       cout << "Result: " << sub << endl;
    }

    else if (answer == 3)
    {
       cout << "Result: " << mul << endl;
    }

    else if (answer == 4)
    {
       cout << "Result " << div << endl;
    }

    else
    {
       cout << "Bad input: Self Destruct in 3....2....1.." << endl << endl;
    }

    while(1){ }
    return 0;

}
Last edited on
You can use a std::string like this:
1
2
3
4
5
6
std::string answer;
cin >> answer;
if (answer == "add")
  // handle add
else if (answer == "sub")
  // handle sub 

http://www.learncpp.com/cpp-tutorial/4-4b-an-introduction-to-stdstring/
Thank you Thomas, i did not realize i needed to quote. It works fine now.
Topic archived. No new replies allowed.