Jul 26, 2008

Enums

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

free web hosting

Enums

kvarnerexpress
if i'm using enums, i can use it as the variable name and as a numerical value, correct?
ex:
enum dayOfWeek {monday, tuesday, wednesday};
dayOfWeek day;

day=monday;
or
day=1;

or it can only be used one way?
please help

Reply

miCRoSCoPiC^eaRthLinG
QUOTE(kvarnerexpress @ Feb 21 2005, 03:54 AM)
if i'm using enums, i can use it as the variable name and as a numerical value, correct?
ex:
enum dayOfWeek {monday, tuesday, wednesday};
dayOfWeek day;

day=monday;
or
day=1;

or it can only be used one way?
please help
*




I believe you refer to the enum list using the variables that you have specified inside.. I'm not sure whether you can refer to it using the index number.. i.e. i've always used it in the form: day = monday, though I'd say there's no reason why you shouldn't be able to use an integer instead of the variable name... If you want more details on enum, follow these links:

1. http://www.functionx.com/cpp/Lesson07.htm
2. http://cplus.about.com/od/learnin2/l/aa082104e.htm
3. http://www.cplusplus.com/doc/tutorial/tut3-6.html
4. http://cplus.about.com/od/beginnerctutorial/l/aa021702a.htm

Hope this helps smile.gif

 

 

 


Reply

osknockout
I think it's only defined in a hash format (day="monday")
but can be made for an index format. Last time I was
at odds with the issue, it changed from standard to standard
even across compilers. Even we sages do not know now biggrin.gif
... so tell us what you find.

Reply

dexter
Well since dayOfWeek is now a new type, to be able to make day = 1 work, you actually have to use,

day = (dayOfWeek)1;

The question that begs to be asked is "Why?". Enums are there to make your life easier, in both writing and maintaining the code (meaning you don't have to remember exact values, just names... much easier.

But basically, they are just like declaring some constants and using them with an integer...

CODE

const monday = 1;
const tuesday = 2;
const wednesday = 3;

int main()
{
 int day = monday;

 if(day == monday)
 {
       ....



You get the picture... using enums is just a simpler way of doing that.

Also, I might mention that enums start at 0 usually by default, but you can change that by declaring... (in this case), enum dayOfWeek {monday = 1, tuesday, wednesday };


Hope that answers your question.

Reply

fffanatics
It would work both ways but for one it is normally common practice to make all enums capital because they act as constants. Ex. enum daysofWeek {MONDAY};

Secondly, you can not use it like today = 0; you would have to cast the 1 to a daysofWeek like today = (daysofWeek)0;

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : enums


    Looking for enums

Searching Video's for enums
advertisement



Enums



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE