Sign in to follow this  
voyager838

convert string to enum

Recommended Posts

voyager838    122
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
SiCrane    11839
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
Grain    500
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
SiCrane    11839
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
OrangyTang    1298
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[i])
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   
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
OrangyTang    1298
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   
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
Guest Anonymous Poster   
Guest Anonymous Poster
by the way
if you have to deal with flags and convert string to an enum
then one solution can be likely

also a pseudo code ;)


enum CAVE
{
MAP1 = 1 <

Share this post


Link to post
Share on other sites
voyager838    122
sorry about that error...

it seem to be standing

by the way
if you have to deal with flags too and convert string to an enum
then one solution can be likely

also a pseudo code ;)

enum CAVE
{
MAP1 = 1, // binary mask 0001
MAP2 = 2, // binary mask 0010
MAP3 = 4, // binary mask 0100
MAP4 = 8 // binary mask 1000
};


unsigned char convStr2Enum_flag (char *str)
{
unsigned char i = 0;
if (strstr(str,"MAP1")!=0)
i = MAP1;
if (strstr(str,"MAP2")!=0)
i |= MAP2;
if (strstr(str,"MAP3")!=0)
i |= MAP3;
if (strstr(str,"MAP4")!=0)
i |= MAP4;
return i;
}

and it dosent matter in what order you put this flag either.
example
MAP3|MAP2|MAP4 will give the result 14 and it respond to the mask 1110

/Voy

Share this post


Link to post
Share on other sites
Inmate2993    222

enum {
CAVE_ROCK,
CAVE_SPIKE,
CAVE_YOURMOTHER,
CAVE_EOF
};

const char * cave_strings[] =
{
"Rock",
"Spike",
"YourMother",
"#"
};

int StringToEnum( const char * pstr )
{
for (int i=CAVE_ROCK; i<CAVE_EOF; i++)
{
if (0==strcasecmp(pstr, cave_strings[i])) return i;
}
return CAVE_EOF;
}

Fun stuff.
Actually, this may not work if your compiler can't do the cave_strings initializer, as this is a GCC extention of C, not ansi C. Plus that enum might be suspicious.

Share this post


Link to post
Share on other sites
DuFaceTheBass    208
Storing the flag as an integer would be nice and easy. Then you could just read an integer from the file and cast it to your enum, like so:


enum CAVE
{
ROCK = 1,
STONE = 2
};


ifstream fin("info.txt",ios_base::in);
int n;
fin >>; n;
CAVE cave = (CAVE)n;

Share this post


Link to post
Share on other sites
voyager838    122
thanks all

it was really interesting problem.

The thing is maby very easy if you in the textfile are writing numbers
instead of the name in enum. but can it exist a simple solution
if i want to read a string from the textfile and then convert it
to the right number in enum?.

i havent found a simple solution for that yeat.

// greetings
voy

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this