Jump to content
  • Advertisement
Sign in to follow this  
voyager838

convert string to enum

This topic is 4723 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

sorry <correction> Hi everyone.¨ can someone help me to get an idea how to convert a string to enum type. exempel enum CAVE { ROCK = 1, STONE = 2 }; and then when i read in a string from lets say a textfile like ifstream fin("info.txt",ios_base::in); char str[20]; fin >> str; and try to convert it like unsigned int MyFlag = CAVE(str); but it dosent work... what should i do? im newbie so i hope someone has a clue, what to handle with this kind of problem. most gratefully voy

Share this post


Link to post
Share on other sites
Advertisement
At runtime the identifiers used to designate enums don't exist. If you want a mapping between strings and enums you have to implement it manually. One approach is to use a std::map. Ex:

std::map<std::string, CAVE> CAVE_map;
CAVE_map["ROCK"] = ROCK;
CAVE_map["STONE"] = STONE;

CAVE value = CAVE_map[str];

Share this post


Link to post
Share on other sites
You could simply use a switch statment or series of if, else ifs.


enum pets
{
CAT,
DOG,
FISH,
HAMSTER,
SNAKE
}

int MyPet;

std::ifstream File(FileName);
std::string Enum;
File >> Enum;

if(Enum == "CAT")MyPet= CAT;
if(Enum == "DOG")MyPet= DOG;
....

Share this post


Link to post
Share on other sites
Quote:
Original post by Grain
You could simply use a switch statment or series of if, else ifs.

It's illegal to do a switch statement on strings in C++.

Share this post


Link to post
Share on other sites
Quote:
Original post by Grain
You could simply use a switch statment or series of if, else ifs.

That won't work in C++. If you really want to avoid using a std::map then you can use a parrallel array.

(pseudo code):

enum CAVE
{
ROCK = 0;
STONE,
CHEESE,
LAST_CAVE = CHEESE
}
string[LAST_CAVE] CAVE_STRS = { "Rock", "Stone", "Cheese" };

stringToEnum(string str)
{
for (int i=0; i<LAST_CAVE; i++)
{
if (str.equals(CAVE_STRS)
return i;
}
// Not found!
assert (false);
}

The only snag being that you have to be careful to keep your enum and string array in sync, otherwise you'll convert wrongly.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
yes,
good idea,
But it seems that string class dosent include the member equals ??

and second,

it proberly gonna work for numbers that is in serie. like 0 1 2 3 4 and so on.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
yes,
good idea,
But it seems that string class dosent include the member equals ??

Like I said, that's only pseudo code, at least put some effort in yourself. [razz] If you're using std::string IIRC it has an overloaded == operator. Else, strcmp.

Quote:
it proberly gonna work for numbers that is in serie. like 0 1 2 3 4 and so on.

Well unless you're deliberatly messing with it, enums always are. If you need values that start other than zero, add an offset (similar to LAST_CAVE). If you need non-continuous enum values then you're going to have to go with a std::map.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
like i said before...
im newbie so please be patient, thats explain why i have to ask
about members like equals. seems logic?
http://www.gamedev.net/community/forums/images/icons/icon91.gif
http://www.gamedev.net/community/forums/images/icons/icon91.gif

thanks for the advice anyway.

I think i found a solution in the end.
and it ends up in something like

(also a pseudo code)

enum CAVE
{
MAP1 = 12,
MAP2 = 45
};

int convertStr2Enum(char *str)
{
if (strcmp(str,"MAP1")==0)
return MAP1;
if (strcmp(str,"MAP2")==0)
return MAP2;
// nothing
return -1;
}

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!