switch statments :switch quantity not an integer

need some help im writing a class and the constructor for it is:
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
Token::Token(string passedValue)
{
	value = passedValue;
	switch (passedValue)
	{
		case "-":
		case "+":
			type = "binary operator";
			precedence = 1;
			break;
		case "*":
		case "/":
			type = "binary operator";
			precedence = 2;
			break;
		case "sin":
		case "cos":
		case "sqrt":
			type = "unary function";
			precedence = 3;
			break;
		case "^":
			type = "exponentiation";
			precedence = 4;
			break;
		case "(":
			type = "left parentheses";
			precedence = 5;
			break;
		case ")":
			type = "right parentheses";
			precedence = 5;
			break;
		case "x":
			type = "variable";
			precedence = 0;
			break;
		default:
			type = "literal";
			precedence = 0;		
	}


heres my main function that i used to test my class.
1
2
3
4
5
6
7
8
9
10
int main()
{
	string me = "+";
	Token mytoken(me);
	
	cout << mytoken.getValue();
	cout << mytoken.getPrecedence();
	cout << mytoken.getType();
	return 0;
}

i was testing it out but i keep getting error "switch quantity not an integer".
does this mean i can't use switch with string? if there any way to do this? i really don't want to use if else statements.
You can't. You have to use if/elses in this case.
yea i ended up doing that it worked... i didn't want to though, its so tedious and looks ugly.
Switch on a string:
http://www.cplusplus.com/forum/general/11460/#msg54095
(Scroll down to the second half of my post)

Hope this helps.

[edit]
BTW, you could switch on the first character of the string:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
switch (passedValue[ 0 ])
{
    case '-':
    case '+':
        ...
    case '(':
    ...
    case 's':
    case 'c':
        if      (passedValue == "sin") ...
        else if (passedValue == "cos") ...
        else if (passedValue == "sqrt") ...
        else ...
        break;
    ...
}

Good luck!
Last edited on
Well you could create an enum in your main code (eg: enum variables{'-'=0,'+'=1,...}) and then just comment the enum assignments in the switch statement for readability.
@stavros, that's a good idea. i will try that. but switch accepts enum types as its parameter?
Switch accepts anything which default-promotes to an integer value, including an enum.

You cannot name enumerated values with character literals. An enumerated value must be identifiers, just like every other value name in your program...

...Which leads back to the original problem: how to select based upon a string value? I've given you two answers.

To involve an enum is only useful if you plan to use them as magic values which are used in multiple places in your program. Otherwise it is overhead.
so that means is use enum to create a token type and pass the an object of type token to switch, it will work?
Well, enum types default to a number, don't they?
did you get this resolved? if so, can you mark it as solved.
Topic archived. No new replies allowed.