Link to home
Start Free TrialLog in
Avatar of gname
gname

asked on

mapping enums to sring

Hi ,
I have around 500 enums , which I need to print as string vlaue.
for example  -
enum {
Jan =1,
Feb,
March,
April
}
int month =2
switch(month)
case Jan:
cout <<January <<endl;
case Feb...
cout <<Feburay <<endl

What is best way to map enum to strings, becasue switch case is too much for 500 enums ?

Thanks
Avatar of jaiminpsoni
jaiminpsoni
Flag of India image

Or may be simple lookup table?

Or std::map may also help...
Avatar of gname
gname

ASKER

I can prepare a map and then search, but the question here is - how to insert string as enum vlaue names in the map? I dont want to do one by one mapping for all 500 vlaues.  is there any template method etc ??
OK... So, I am not sure if I really understood your question....

I thought you wanted to make retrieval efficient... but here it seems you want to make "writing the code" as efficient.

Are you looking for something like this?

http://www.cplusplus.com/forum/general/2949/

Hi, you can use an array of strings to store names.  I illustrate one example in the following code. The other way would be store all the names in a file and read them into an array. I can write a code if you want the solution reading from a file. Hope it helps.

enum Month {Jan=1, Feb, Mar};
char* name[] = {"January","February","March"};

int main() {
  Month  m= Jan;
  printf("The month is %s", name[m - 1]);
}
ASKER CERTIFIED SOLUTION
Avatar of TomasP
TomasP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike McCracken
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.