469,934 Members | 2,479 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

enum

13
Hi there,

I'm fairly new at this, and I am having a bit of trouble wrapping my head around some concepts of enum for a project. So any help would be greatly appreciated.

Essentially I'm writing a program where i will have an enum in one of my classes. When the program is used, the user will define what actually goes inside the enum.
So for example what I mean is if the class is WorkDay....and the user defines what days of the weeks he or she works. One user would say monday, tuesday and thursday...and the class would look like:

class WorkDay{
enum days { monday, tuesday, thursday };
}

Another user might say monday, wednesday, thursday and friday....and the class would look like:

class WorkDay{
enum days { monday, wednesday, thursday, friday};
}


My question is....well....how do I actually code that?
How do I define what goes inside the enum each time the program is run through the user's input??

Another question is....how do I assign the enum constants to variables when it's inside a class?
Do I use something like:

WorkDay enum today = sunday; //???

And one final question....how do I actually output the enum constant?
If I want to output the above variable 'today'...it has already been enumerated...so it would output an number. But what if I want to output the word 'sunday' instead?


Sorry to bombard everyone with all of these questions. I'm just really confused with the basic concepts right now.

Thanks a lot for your help.
Apr 10 '07 #1
4 1698
gpraghuram
1,275 Expert 1GB
Hi,
To clarify ur first question
1)enums have to be specified at compile time and cant be specified on the go during run time
2)You can maintain a map to do this.


Thanks
Raghuram
Apr 10 '07 #2
Banfa
9,065 Expert Mod 8TB
And one final question....how do I actually output the enum constant?
If I want to output the above variable 'today'...it has already been enumerated...so it would output an number. But what if I want to output the word 'sunday' instead?
Almost everytime I create an enum I also create a function that takes the enum as an input and returns a const char * with the textual name of the enum entries to facilitate output and debugging.
Apr 10 '07 #3
cpp
5
Hi,

Answer to your second question."how do I assign the enum constants to variables when it's inside a class?"

you can do it inside the constructor of that class..just like assigning any normal variable a value

answer to your last Q:
You can use a switch statement, where cases include your enumerated consts and for each const you can print corresponding name
Apr 10 '07 #4
ice8595
13
thank you!!
Apr 11 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

20 posts views Thread by Glenn Venzke | last post: by
21 posts views Thread by Andreas Huber | last post: by
31 posts views Thread by Michael C | last post: by
18 posts views Thread by Visual Systems AB \(Martin Arvidsson\) | last post: by
2 posts views Thread by Dennis | last post: by
1 post views Thread by Randy | last post: by
2 posts views Thread by Randy | last post: by
34 posts views Thread by Steven Nagy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.