switch and string/char

Is there a way to use a switch statement with string or char instead of an int?
It is just the same.
char, yes. String, no.
switch can only be used with integer types.
@TheIdeasMan
You are wrong. Try it yourself. It won't work.

@helios
Can you show me an example with char? Is there not way to do it with strings even using some sort of conversion?
1
2
3
4
switch (x){
    case 'a':
    case 'b':
}
You can only check the equality of a pointer, but probably you won't know until runtime the value of the pointer, so it's going to be pretty useless. You will have to do lots of if/then/else. No way around.

Edit: As helios stated, you can only use switch on integer (enum, Integral) types, which are:
int/unsigned int,
long/unsigned long,
char/unsigned char,
wchar_t.
NO more.
Last edited on
Topic archived. No new replies allowed.