Solved

mapping enums to sring

Posted on 2011-03-22
8
335 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:gname
8 Comments
 
LVL 5

Expert Comment

by:jaiminpsoni
ID: 35192594
0
 
LVL 5

Expert Comment

by:jaiminpsoni
ID: 35192683
Or may be simple lookup table?

Or std::map may also help...
0
 

Author Comment

by:gname
ID: 35194146
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 ??
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:jaiminpsoni
ID: 35196177
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/

0
 
LVL 1

Expert Comment

by:thangdn
ID: 35246861
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]);
}
0
 
LVL 5

Accepted Solution

by:
TomasP earned 500 total points
ID: 35317870
To save all of the typing use your preprocessor to directly turn the text for each enum value into a string
#define TOSTRING( aenum ) # aenum

enum {
Jan =1,
Feb,
March,
April
}


To leverage the proprocessor just use TOSTRING like this:

TOSTRING(Feb);   // This will resolve to the text 'Feb'
You can use this directly or declare a table.

You can populate a string table in a loop without typing it all in by following the "tricks" used here
http://www.edm2.com/0405/enumeration.html

Once you get the framework up, you will reuse this without a doubt, I do


0
 
LVL 100

Expert Comment

by:mlmcc
ID: 36275168
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now