Thread: enumeration types

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    Question enumeration types

    I am a struggling toyer with C and I've worked my way up to enumeration types and typedef and now I'm stuck. here is my code:

    #include<stdio.h>

    enum month{jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
    typedef enum month month;


    int main()
    {
    month d,m,x;
    system("cls");
    printf("Enter number of the month: ");
    scanf("%d", &d);
    printf("Current\t\tPrevious\n\n");
    m=find_prev_month(d);
    printf("\t\t%c\n",m);
    return 0;
    }


    month find_prev_month(month d)
    {
    char x;

    month prev_month;
    if(d == 1)
    x = "January";
    switch (d)
    {
    case jan:
    printf("%c\t\tDecember", x);
    break;
    case feb:
    printf("\t\tJanuary");
    break;
    case mar:
    printf("\t\tFebruary");
    break;
    case apr:
    printf("\t\tMarch");
    break;
    case may:
    printf("\t\tApril");
    break;
    case jun:
    printf("\t\tMay");
    break;
    case jul:
    printf("\t\tJune");
    break;
    case aug:
    printf("\t\tJuly");
    break;
    case sep:
    printf("\t\tAugust");
    break;
    case oct:
    printf("\t\tSeptember");
    break;
    case nov:
    printf("\t\tOctober");
    break;
    case dec:
    printf("\t\tNovember");
    break;
    }
    return prev_month;
    }

    and here is my output:

    Enter number of the month: 1
    Current Previous

    - December


    What I would like for it to do is to ask for the number of the month, then take that data, print a table of the current month that was selected and the previous month, then under that, the rest of the months in order, something along these lines if say 7 for July was entered:

    Current Previous
    July June
    August July
    September August

    and so on for all 12 months. I am by no means a professional programmer, just trying to learn to better myself. HELPPPPP!!!! Thanking you all in advance.....

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    Unhappy enumeration types

    I'm thinking on that code that I could use an array or something like that. I was also possibly thinking that I could use an IF statement, something like:

    if(d1==d2)
    printf("xxxxx");
    else if(d1+1==d2);

    Would something like that work?

  3. #3
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125

    Wink

    I am at work now, so I don't really have time to debug you code, but I would try the following:

    Instead of

    enum month{jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
    typedef enum month month;

    try

    typedef enum _month_ {
    jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
    }month;
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    #include<stdio.h>
    
    enum month{jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
    typedef   enum month   month;
    
    month find_prev_month ( month d );
    
    char *month_names[] = {
        "December",     // this is month 0 - ie, jan-1
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December",
    };
    
    int main() {
        month d,m;
        printf("Enter number of the month: ");
        scanf("%d", &d);
        m=find_prev_month(d);
        printf("Current\t\tPrevious\n");
        printf("%s\t\t%s\n",month_names[d],month_names[m]);
        return 0;
    }
    
    month find_prev_month ( month d ) {
        return d-1;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    enumeration types

    Salem;

    Thanks alot! That was a tremendous help. So the array did work. To get it to print the rest of the months under the top two, I could put in a FOR loop correct? Like:

    for(i=0;i>12;++i)


    and run them thru this way? Once again, thanks alot!!

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > for(i=0;i>12;++i)

    Um, no. This will fail on the first check.

    i==0 is NOT greater than 12. You mean:

    for(i=0;i<12;i++)

    Quzah.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    whoops

    Yeah, i knew that, it was a stupid typo.....makes me really look like a rookie huh?

    anyway, as far as the entire output of every month, would I be able to use it that way? I mean, it prints out the previous month, plus the month that I entered, but not the subsequent months as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 12:57 PM
  2. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. Enumeration Issues
    By cdn_bacon in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2007, 02:26 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM