Problems with "switch"

Ok, so a few days ago I wrote a calculator with the if-else-if ladder, and decided to edit it to use switch, but it's not working. It always defaults to "Invalid Input", even if I input a valid value. Here's the code:
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
#include <iostream>
#include "Functions.h"
using namespace std;
double add(double x, double y);
double mult(double x, double y);
double sub(double x, double y);
double divide(double x, double y);
int main ()
{
    double input1, input2;
    char op;
    cout << "Input a number: ";
    cin >> input1;
    cout << "Input 1 for multiply, 2 for add, 3 for subtract, or 4 for divide: ";
    cin >> op;
    switch (op)
    {
case 1:
    cout << "Input number to multiply by: ";
    cin >> input2;
    cout << "The result is " << mult(input1,input2) << ".";
    break;
case 2:
    cout << "Input number to add by: ";
    cin >> input2;
    cout << "The result is " << add(input1,input2) << ".";
    break;
case 3:
    cout << "Input number to subtract by: ";
    cin >> input2;
    cout << "The result is " << sub(input1,input2) << ".";
    break;
case 4:
    cout << "Input number to divide by: ";
    cin >> input2;
    cout << "The result is " << divide(input1,input2) << ".";
    break;
default: 
         cout << "Invalid input.";
         break;
}
cin.get();
cin.get();
return 0;
}


Can anyone please explain what I'm doing wrong?
'op' is a char so it will contain the ASCII value of the input digit. You can change it to an integer or use character constant ( '1' ) instead of numeric literals ( 1 ) for the case labels
Ohh. Okay. Thank you very much. I changed the case labels to character constants, and everything works fine now. Thanks again!
you could then change the 1,2,3,4 to *,+,-,/ to make it easier/more user friendly/more natural if you wanted, since you're using character constants...
also,
1
2
cout << "Enter equation: ";
cin >> input1 >> op >> input2;

would allow the user to input "1 * 2" (all at once)

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
#include <iostream>
#include "Functions.h"
using namespace std;
double add(double x, double y);
double mult(double x, double y);
double sub(double x, double y);
double divide(double x, double y);
int main ()
{
    double input1, input2;
    char op;
    cout << "Enter equation: ";
    cin >> input1 >> op >> input2;
    switch (op){
        case '*':
            cout << "The result is " << mult(input1,input2) << "." << endl;
            break;
        case '+':
            cout << "The result is " << add(input1,input2) << "." << endl;
            break;
        case '-':
            cout << "The result is " << sub(input1,input2) << "." << endl;
            break;
        case '/':
            cout << "The result is " << divide(input1,input2) << "." << endl;
            break;
        default: 
            cout << "Invalid usage! Format is \"firstNumber operator secondNumber\"" << endl;
            break;
    }
    cin.get();
    cin.get();
    return 0;
}
Topic archived. No new replies allowed.