Public Group

# switch()

This topic is 4566 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Dang, I just found that I can't use strings in a switch statement. What can I do to work around this? I tried .c_str() but that doesn't work, presumably because that changes a string to a char* and not an int, but I thought I'd try it anyway. What do I do for this? If there isn't a simple solution, I'll just use a bunch of ifs and else ifs, but now I'm curious and wish to know how to get it to work. Thanks! -Servant of the Lord

##### Share on other sites
You can create a std::map<std::string, something> to map strings to integers that you can switch on or function objects that you can invoke.

##### Share on other sites
Quote:
 Original post by SiCraneYou can create a std::map to map strings to integers that you can switch on or function objects that you can invoke.

How do I do that? My string is called 'Input' so I would go:

std::map<Input, N_Input>switch(N_Input){    case "north" || "n":     ...

And do I need to include special headers, or is it in iostream?

##### Share on other sites
He's saying have a map that converts "north" to ID_DIRECTION_NORTH or something like that which is a number. Then switch on the number.

If there are only a few possible strings I would just use a bunch of if/else statements. If there are a large number you could do something like map a string to a function pointer but that's more complicated.

##### Share on other sites
Quote:
 Original post by Anon MikeHe's saying have a map that converts "north" to ID_DIRECTION_NORTH or something like that which is a number. Then switch on the number.If there are only a few possible strings I would just use a bunch of if/else statements. If there are a large number you could do something like map a string to a function pointer but that's more complicated.

I currently have 37 possible strings, so I'll try and map them. I still don't quite understand how this is done though, so could someone show me how to or point me to a small tutorial?

##### Share on other sites
Something like this:
enum Direction{   North,   South,   West,   East};std::map<Direction, std::string> stringMap;// In init code:stringMap[North] = "North";stringMap[South] = "South";stringMap[West] = "West";stringMap[East] = "East";// When you check the values:std::map<Direction, std::string>::iterator it = stringMap.find(input);if(it == stringMap.end()){   // Invalid input, can't find this string in the map (like the "default" case statement)}switch(it.first){   case North:      break;   case South:      break;   case West:      break;   case East:      break;}

Or something like that. There may be a better way to do it, I'm not sure. It's been ages since I used std::map...

##### Share on other sites
Thanks. That seems rather chunky for what seems like a simple thing. Someone should create a varation of switch which acepts most variables. I'll mess around with what you've given me though, and see if I can understand it and make switching simpler.

Thanks for all your help everyone.

##### Share on other sites
Quote:
 Original post by sYn pHrEAkC++ map reference

Thanks.

##### Share on other sites
I'd go the other way, and map strings to directionids, not the other way around.

enum{    NORTH,    EAST,    SOUTH,    WEST};std::map<std::string,int> the_map;the_map["NORTH"]=NORTH;the_map["N"]=NORTH;the_map["EAST"]=EAST;the_map["E"]=EAST;the_map["SOUTH"]=SOUTH;the_map["S"]=SOUTH;the_map["WEST"]=WEST;the_map["W"]=WEST;std::string command = getCommand();std::map<std::string,int>::iterator iter = the_map.find(command);if(iter != the_map.end() ){    switch(iter->second){        case NORTH:            //go north            break;        case EAST:            //go east            break;        case SOUTH:            //go south            break;        case WEST:            //go west            break;        default:            //unknown command    }    }

Note: you'll need to either come up with a case insensitive string compare, or else ensure that your strings are first converted to the proper case.

1. 1
2. 2
Rutin
21
3. 3
4. 4
A4L
15
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633741
• Total Posts
3013624
×