Enumeration help

This is a discussion on Enumeration help within the C Programming forums, part of the General Programming Boards category; I'm supposed to be using enumeration to print a list of the months, and next to each month, the month ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    20

    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;			
    		}
    	}

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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);
    	}
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    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

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    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

    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.
    Last edited by Polymorphic OOP; 04-17-2003 at 10:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding/Implementing Enumeration
    By Iconate in forum C Programming
    Replies: 15
    Last Post: 10-10-2008, 10:16 AM
  2. Enumeration Issues
    By cdn_bacon in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2007, 03:26 PM
  3. Enumeration problem
    By baniakjr in forum C++ Programming
    Replies: 8
    Last Post: 11-11-2006, 02:32 PM
  4. Having trouble implementing enumeration.
    By RP319 in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2005, 07:03 PM
  5. enumeration
    By C-Struggler in forum C Programming
    Replies: 5
    Last Post: 03-13-2003, 09:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21