switch()

Started by
36 comments, last by mikeman 17 years, 10 months ago
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
Advertisement
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.
Quote:Original post by SiCrane
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.


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?

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.
-Mike
Quote:Original post by Anon Mike
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.

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?
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...
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.
C++ map reference

Quote:Original post by sYn pHrEAk
C++ map reference


Thanks.
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.

Get off my lawn!

This topic is closed to new replies.

Advertisement