enum question

Lets say I have an enum with 12 values

 
enum MONTH {Jan = 1, Feb, Mar, Apr, Jun, Jul, Aug, Sep, Oct, Nov, Dec};


And I am asking the user for the date, they input Month (enum type) day (int) and year (int).

Now I have the date, Now with a function I am supose to ask them to change date by adding days.

So lets say the date they entered was

Jan 5 2010

Now they added 367 Days, So that would now make the date Jan 7 2011.

To display the date I am using a switch

example

1
2
3
4
5
6
7
switch(MONTH)
case 1 : realMonth = "January";
etc
etc
etc

so realMonth is a string var to display the date based on the enum.


How can I make the enum once it passses 12 (dec) reset back to jan, so I can use a simple counter that will keep cycleing threw 1-12?

You would do that in your function, not via the enum.

maybe something like while (month> 12) month -=12;
Thanks, Thats what I ended up doing, just making my own function for it. Was hoping there was some kind of enum trick I didnt know of :P
For enums where you don't specify *any* of the values, you can get the number of allowed values by adding a last entry:

enum MONTH { Jan, . . ., Dec, cMONTHS }

Note that Jan = 0, now, and cMONTHS = 12.

Then, you get the month index by month % cMONTHS, which you can encapsulate in a function, if you want.

This tricks helps protect you when you add new enum values (suppose you start to work in Aztec 13 day trecenas, 20 per sacred year). You just add the new enum labels, and all the code still works unmodified.
Topic archived. No new replies allowed.