Thread: Enumeration Question

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    10

    Question 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. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    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. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    String literals should be const to prevent mistakes: http://cpwiki.sourceforge.net/Common...kes_and_errors
    Just pointing out, it is good practice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    Good point, Elysia. Thanks for pointing that out. So:
    Code:
    const *month[] = { "January", "February", etc... };

  5. #5
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by nucleon View Post
    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!
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by nucleon View Post
    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. #7
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by master5001 View Post
    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?
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  8. #8
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by Elysia View Post
    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
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by nucleon View Post
    Good point, Elysia. Thanks for pointing that out. So:
    Code:
    const char* month[] = { "January", "February", etc... };
    Oops, you forgot char in there.

    Quote Originally Posted by Matus View Post
    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;
            }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    They can do both...
    Code:
    enum months_e
    {
      JAN = 0,
      FEB,
      MAR ...
    };
    
    /* ... */
    const char * thingies[] = {"January", "Febraury", ...};
    
    /* ... */
    printf("&#37;s", thingies[m]);

  11. #11
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by zacs7 View Post
    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!!!
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But it is enums... enums and arrays.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by Elysia View Post
    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??
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by Elysia View Post
    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
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enumeration Issues
    By cdn_bacon in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2007, 02:26 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM
  5. Enumeration question?
    By cheesehead in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2001, 06:31 PM