-
Enumeration help
I'm supposed to be using enumeration to print a list of the months, and next to each month, the month that comes before it. Problem is the teacher didn't cover it too much, and the book we use, which is usually pretty good at explaining topics, only has one example and I don't even understand the concept. Can anyone help me out?
Code:
// Homework 4 Problem 1
#include <stdio.h>
enum month {jan=1,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec};
typedef enum month month;
month previous_month(month m);
int main(void)
{
int i;
printf("MONTHS and PREVIOUS MONTHS\n");
for (i=1;i<12;++i)
printf("%d ",month);
month previous_month(month m);
printf("%d",prev_month);
return 0;
}
month previous_month(month m)
{
month prev_month;
switch (m)
{
case jan:
prev_month=dec;
break;
case feb:
prev_month=jan;
break;
case mar:
prev_month=feb;
break;
case apr:
prev_month=mar;
break;
case may:
prev_month=apr;
break;
case jun:
prev_month=may;
break;
case jul:
prev_month=jun;
break;
case aug:
prev_month=jul;
break;
case sep:
prev_month=aug;
break;
case oct:
prev_month=sep;
break;
case nov:
prev_month=oct;
break;
case dec:
prev_month=nov;
break;
return prev_month;
}
}
-
Code:
enum Month {Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
Month PreviousMonth(Month CurrentMonth)
{
if(CurrentMonth == Jan)
{
return Dec;
}
else
{
return (Month)(CurrentMonth - 1);
}
}
-
I think that helped, but I'm having trouble figuring out where that would go and what I need to delete. Would it be possible to explain that a bit more? TIA
-
It just replaces that previous_month function
You can also just do
Code:
month previous_month(month m)
{
return m != jan ? (month)(m-1) : dec;
}
yay :D
All it does is it returns what the month was prior to the month input to the function. So If you did previous_month( jul ), then it would return jun, etc.