convert string to enum

Started by
13 comments, last by voyager838 18 years, 9 months ago
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
-= Code should be simple to understand =-
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];
thanks
-= Code should be simple to understand =-
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;....
thanks again,
that seems more easy to understand...
-= Code should be simple to understand =-
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++.
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.
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.

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.
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;
}

This topic is closed to new replies.

Advertisement