# Enumeration Question

This is a discussion on Enumeration Question within the C Programming forums, part of the General Programming Boards category; This program will tell you what the next day is. You give it the day and month and it will ...

1. ## Enumeration Question

This program will tell you what the next day is. You give it the day and month and it will do all of the end of month and end of year calculations correctly. The only problem is that I want to use an enumeration to make the program length shorter. I have an enum, month, and typedef, but don't know how to insert it into the program correctly.

Code:
```#include <stdio.h>

enum month {January=1, February=2, March=3, April=4, May=5,
June=6, July=7, August=8, September=9, October=10,
November=11, December=12};

typedef   enum month   month;

int main(void)
{
int d = 0, m = 0;

printf("\nEnter a day number (1~31): ");
scanf("%d", &d);

printf("\nEnter a month number (1~12): ");
scanf("%d", &m);

if ( ((m==1||m==3||m==5||m==7||m==8||m==10||m==12) && (d==31)) ||
((m==4||m==6||m==9||m==11) && (d==30)) ||
((m==2) && (d==28))  )
{
if((m==1||m==3||m==5||m==7||m==8||m==10||m==12) && (d==31))
{d = 1; m += 1;}
if((m==4||m==6||m==9||m==11) && (d==30))
{d = 1; m += 1;}
if((m==2) && (d==28))
{d = 1; m += 1;}
}
else {d += 1;}

if(m==13)
{m = 1;}

printf("\n%s%d", "The next day is: ", d);
printf("\n%s", "The next month is: ");

switch(m)
{
case 1: printf("January"); break;
case 2: printf("February"); break;
case 3: printf("March"); break;
case 4: printf("April"); break;
case 5: printf("May"); break;
case 6: printf("June"); break;
case 7: printf("July"); break;
case 8: printf("August"); break;
case 9: printf("September"); break;
case 10: printf("October"); break;
case 11: printf("November"); break;
case 12: printf("December"); break;
}
printf("\n");
}```

If anyone could help me add it in and achieve the same result, that would be cool.

P.S. > Sorry if the code is hard to read/follow! <

2. I don't think you want an enumeration (not if you want the code shorter). An array of the month names would do the trick.
Code:
`char *month[] = { "January", "February", etc... };`
Then your switch can be replaced with:
Code:
`printf( "&#37;s", month[m - 1]);`
Also, an array would make the huge if condition neater. So this:
Code:
```if ( ((m==1||m==3||m==5||m==7||m==8||m==10||m==12) && (d==31)) ||
((m==4||m==6||m==9||m==11) && (d==30)) ||
((m==2) && (d==28))  )
etc...```
would be this:
Code:
```int days_in_month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
...
if (d == days_in_month[m - 1])
{
d = 1;
if (++m == 13) m = 1;
}
else
++d;```
Although note that this does not handle leapyears.

3. String literals should be const to prevent mistakes: http://cpwiki.sourceforge.net/Common...kes_and_errors
Just pointing out, it is good practice.

4. Good point, Elysia. Thanks for pointing that out. So:
Code:
`const *month[] = { "January", "February", etc... };`

5. Originally Posted by nucleon
I don't think you want an enumeration (not if you want the code shorter). An array of the month names would do the trick.
THe reason to this is because the instructions were to use enum, thats part of the instructions. I guess this person is in my class, however, this class is just plain old darn i dnt know constipated when it comes to working together. If u want to try help, they say u cheat, and if u dnt they say u dnt want to. The class is just complicated and its five of us that are left. Between Marth, that code could be improved just read our class book it has all that u need, iv done that one for the extra points too!

6. Originally Posted by nucleon
Code:
`const *month[] = { "January", "February", etc... };`
Her point was a good one, yes. But your implementation of her good point assumes that you have a "psychic" extention built into your compiler.

7. Originally Posted by master5001
Her point was a good one, yes. But your implementation of her good point assumes that you have a "psychic" extention built into your compiler.
declarations huh?

8. Originally Posted by Elysia
String literals should be const to prevent mistakes:
Elysia i still wonder on the mysteriousness srsly, why so mysterious. The previous thread honestly kinda creeped me out , thanks to the other poster, forgot his user

9. Originally Posted by nucleon
Good point, Elysia. Thanks for pointing that out. So:
Code:
`const char* month[] = { "January", "February", etc... };`
Oops, you forgot char in there.

Originally Posted by Matus
Elysia i still wonder on the mysteriousness srsly, why so mysterious. The previous thread honestly kinda creeped me out , thanks to the other poster, forgot his user
Because I like being mysterious, that is all. Why provide details when you can hide on the web?

As for the original OP: All that enums would do is make your code more readable, not shorter code. The original code:
Code:
```        switch(m)
{
case 1: printf("January"); break;
case 2: printf("February"); break;
case 3: printf("March"); break;
case 4: printf("April"); break;
case 5: printf("May"); break;
case 6: printf("June"); break;
case 7: printf("July"); break;
case 8: printf("August"); break;
case 9: printf("September"); break;
case 10: printf("October"); break;
case 11: printf("November"); break;
case 12: printf("December"); break;
}```
Can be changed to the following with enums:
Code:
```        switch(m)
{
case January: printf("January"); break;
case February: printf("February"); break;
case Mars: printf("March"); break;
case April: printf("April"); break;
case May: printf("May"); break;
case June: printf("June"); break;
case July: printf("July"); break;
case August: printf("August"); break;
case September: printf("September"); break;
case October: printf("October"); break;
case November: printf("November"); break;
case December: printf("December"); break;
}```

10. They can do both...
Code:
```enum months_e
{
JAN = 0,
FEB,
MAR ...
};

/* ... */
const char * thingies[] = {"January", "Febraury", ...};

/* ... */
printf("&#37;s", thingies[m]);```

11. Originally Posted by zacs7
They can do both...
Code:
```enum months_e
{
JAN = 0,
FEB,
MAR ...
};

/* ... */
const char * thingies[] = {"January", "Febraury", ...};

/* ... */
printf("%s", thingies[m]);```
THe point is to use ENUMS not arrays, hence what u are saying is something like this:

Code:
```if (d == days_in_month[m]){

d = 1;

++m;

if (m == 13) m = 1;

}

else

++d;

printf( "%s%d", months[month],day);```
Like what the guy above said, this simple piece of code replaces all that huge piece of the original code with this simplicity. But the idea behind this assignment is not arrays But ENUMS!!!

12. But it is enums... enums and arrays.

13. Originally Posted by Elysia
But it is enums... enums and arrays.
His i dnt see, m is from the scanf and the thingies is the array. Where is the enum implemented??

14. m should really be of type month, not int, but in C, it matters little since both are just ints. Enums are just a way to map integers to names. No type-safety at all (read: they're a joke).

15. Originally Posted by Elysia
m should really be of type month, not int, but in C, it matters little since both are just ints. Enums are just a way to map integers to names. No type-safety at all (read: they're a joke).
Yea now that would make sense, got it