why won't char work for + - * /?

Hi,
I'm pretty new at c++ and I was making a calculator and tried to have the user enter an algorithm but when I make the variable:
char algorithm = +;
It always comes up with this error message:
expected primary-expression befor ';' token.
Can you help please?

-lpj.



+ isn't a character.

'+' is a character.

(Learn to separate program data from the program itself.)
Hope this helps.
Yes however is there a way the user could just enter + or - when using instead of '+' or '-'?
Your question doesn't really make sense.... but I guess the answer would be "no".

The user inputs text. You can take that text as one or more series of characters... such as '+' or '-' (each of which is a character).

What exactly are you trying to do? Maybe if we can see the bigger picture we can help more.
Last edited on
If you do something like cin>>ch; //ch is of type char the user would not need to include the quotes in his input.

Aceix.
I think the op is trying to do something like
1
2
3
4
std::cout << "Please enter an equation (number operator number): ";
std::cin >> lhs >> opr >> rhs;

result = lhs opr rhs;
but they would need something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
std::cout << "Please enter an equation (number operator number): ";
std::cin >> lhs >> opr >> rhs;

switch(opr)
{
    case '+':
        result = lhs + rhs;
        break;
    case '-':
        result = lhs - rhs;
        break;
    case '*':
        result = lhs * rhs;
        break;
    case '/':
        if(rhs != 0)
            result = lhs / rhs;
        else result = 0; //really undefined
        break;
    default: std::cerr << "Invalid operator." << std::endl;
}
Last edited on
Yes giblit i have that switch all I was asking was what Aceix explained. Thanks for the help!
Last edited on
It takes a while to wrap your head around this stuff but one you do it becomes effortlessly easy, which sometimes makes it hard for those of us who have done this for ages to realize exactly where you are stuck. We're glad to have been of help. (When we can.)
Topic archived. No new replies allowed.