Jump to content
  • Advertisement
Sign in to follow this  
Servant of the Lord

switch()

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
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 this post


Link to post
Share on other sites
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?

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!