C/C++: How to convert int to enum?

Started by
15 comments, last by GameDev.net 18 years, 10 months ago
Quote:Original post by DigiDude
Why not just do this:
today = saturday;


For my app, I couldn't do this because I don't know the day beforehand, this data has to be read from a file.

In my original plan, I was suppose to read the data in as a string from the file, e.g. "saturday", but I didn't know how to convert string to enum. So I thought of using integers to represent days, to make things easier.
today ?= "saturday"

Quote:Original post by Telastyn
Personally, I've moved away from enums due to other reasons and &#111;nly really use them now when the result absolutely never, ever needs displayed or inputed by users or between components. Which is rare to say the least.<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br>How do you convert string to enum, and enum to string?
Advertisement
Couldn't you read it in as integers then use a switch statment like:
int blah = [whatever you read in from file];Days today;switch (blah){case 0:    today = saturday;    break;...}
F-R-E-D F-R-E-D-B-U-R...G-E-R! - Yes!
Quote:Original post by faculaganymede

How do you convert string to enum, and enum to string?


Something like this:
enum hextypes           {NOTHING,GRASSLAND,PLAINS,SPARCEFOREST,FOREST,SWAMP,HILL,MOUNTAIN,DESERT,TUNDRA,JUNGLE,SHALLOWS,OCEAN,HEXTYPECOUNT};       // more later...char *hextypesymbols[]  ={"_","g","p","f","F","w","h","M","d","t","j","-","~"};char *hextypenames[]={"nothing","grassland","plains","sparceforest","forest","swamp","hill","mountain","desert","tundra","jungle","shallows","ocean"};/* snip *//* example */int      x;char     *buf;hextype  ht=NOTHING;// assign buf somehowcout << hextypenames[FOREST];for (x=0;x<HEXTYPECOUNT;++x){     if (buf==strstr(buf,hextypenames[x])){          ht=(hextypes)x;     }}


though using char * is asking for trouble. And editing a source file whenever you want to add a new enum is asking for trouble. And keeping 2-3 arrays in perfect sync is asking for trouble.

So I don't do this any more. I made a templated class which stores a name/ID pair as well as arbitrary properties associated with them. It then can convert nicely between the std::string, the ID, and the properties. Like for the example above, the properties like how much food a hextype yields when farmed is now stored along with the name/ID, rather than a seperate table using the enum as a key. The bonus is that the ID's can be dynamically generated, so the name/ID/properties can be loaded from file or otherwise generated at run-time.

Benefits all around.
To be totally safe just don't make today type Days (plural type names feel so wrong), use a normal number type
enum Days               // Declare enum type Days{   saturday,            // saturday = 0 by default   sunday,          	// sunday = 1   monday,              // monday = 2   tuesday,             // tuesday = 3   wednesday,           // etc.   thursday,   friday}int today;

You can still use the enum constants outside the type & they function exactly as their number counterparts
I often use unnamed enums just for storing constant values, the downside though is that enums can only be intergral
(also unnamed types aren't standard ;])
_______________________________ ________ _____ ___ __ _`By offloading cognitive load to the computer, programmers are able to design more elegant systems' - Unununium OS regarding Python
Quote:Original post by DigiDude
Couldn't you read it in as integers then use a switch statment like:
int blah = [whatever you read in from file];Days today;switch (blah){case 0:    today = saturday;    break;...}


Why would you want to replace what could be a single line of code with a huge switch statement?
Progress is born from the opportunity to make mistakes.

My prize winning Connect 4 AI looks one move ahead, can you beat it? @nickstadb
Quote:Original post by DigiDude
Couldn't you read it in as integers then use a switch statment like:

Thanks. Enum Days is just a quick and dirty example. In the actual app, the list may be very long, so using a switch statement requires a long list of cases. But, it's definitely an idea worth considering.

Quote:Original post by Telastyn
though using char * is asking for trouble. And editing a source file whenever you want to add a new enum is asking for trouble. And keeping 2-3 arrays in perfect sync is asking for trouble.

Yes, I agree. This is very messy. Thanks for sharing :)
Quote:Original post by faculaganymede
Hi all,

Is there a way to convert an int to enum?

For example, I declared the following enum type:
*** Source Snippet Removed ***
Is there a way to set today to Wednesday using an integer, like this:
today = 3


This is legal in C so long as one of the enumerated constants has the value 3.

Quote:
According to C documents, the following is legal, but result is not defined
today = (Days)0;


This is legal in C so long as one of the enumerated constants has the value 0.

C++ may require a cast, I'm not sure.

This topic is closed to new replies.

Advertisement