Thread: need help with enumeration

  1. #1
    Registered User mc088's Avatar
    Join Date
    Jan 2017
    Posts
    19

    need help with enumeration

    Hi,

    the following code (in the first box below) s an example from my C book (except I included the stdafx.h because visual c++ express 2010 needs it to run any c program)

    The output (according to the author of the book) is listed right below the code.

    I tried typing the code into the IDE myself to see that I can get it to run but the compiler shows that there's an error for the month++ part of the for loop. Why is that, seeing that the author of the book shows an output so this must be valid C code?

    "error C2676: binary '++' : 'months' does not define this operator or a conversion to a type acceptable to the predefined operator" is the exact error message shown.

    I thought enum was just a listing of sequential numbers (sort of like an array) but you can reference the values indirectly by way of names in that list) and the ++ should work on it like any other normal variable. Thanks!!




    Code:
    /*  Fig 10.18:
        Using an enumeration type    */
    #include "stdafx.h"
    #include <stdio.h>
    
    
    /* enumeration constants represent months of the year */
    enum months { 
       JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };
    
    
    int main()
    { 
       enum months month; /* can contain any of the 12 months */
    
    
       /* initialize array of pointers */
       const char *monthName[] = { "", "January", "February", "March", 
          "April", "May", "June", "July", "August", "September", "October",
          "November", "December" };
       
       /* loop through months */
       for ( month = JAN; month <= DEC; month++ ) {
          printf( "%2d%11s\n", month, monthName[ month ] );
       } /* end for */
    
    
       return 0;
    }
    Code:
     1 January
     2 February
     3 March
     4 April
     5 May
     6 June
     7 July
     8 August
     9 September
    10 October
    11 November
    12 December
    Last edited by mc088; 02-03-2017 at 07:48 PM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    The problem is that you're compiling the code as C++ instead of C.

    BTW, an enum is not at all like an array. It's more like an int with a bunch of named constants.

  3. #3
    Registered User mc088's Avatar
    Join Date
    Jan 2017
    Posts
    19
    Quote Originally Posted by algorism View Post
    The problem is that you're compiling the code as C++ instead of C.

    BTW, an enum is not at all like an array. It's more like an int with a bunch of named constants.
    Thank you, I got it to work now. I went to the settings and found out where to get the compiler to compile as 'c" and not "c++"".


    Ok, so basically enum is more of an int list with numbers represented by names, if that is the the appropriate way to think about it?



    Also, just wondering,not all C code are valid C++ code then? (just wondering, 'cuz apparently the my "c" code was compiled as "c++" .)

    Thanks!!
    Last edited by mc088; 02-03-2017 at 08:03 PM.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by mc088 View Post
    Ok, so basically enum is more of an int list with numbers represented by names, if that is the the appropriate way to think about it?
    Also, just wondering,not all C code are valid C++ code then?
    C++ is more picky about types. It's complaining about treating the enum as an int. You would need an explicit cast to make it work, something like month = static_cast<months>(month + 1). But you're learning C at the moment so you shouldn't worry about that.

    This
    Code:
    enum State { FIRST, SECOND, THIRD };
    creates a new type called "enum State" that is basically an int (although technically it could be a smaller integer type). At the same time it creates some named constants, automatically assigning them consecutive integers (unless you explicitly assign some).

    It's pretty similar to:
    Code:
    typedef int State;   // Create an alias for int called "State"
    #define FIRST  0     // Create the constants as macros
    #define SECOND 1
    #define THIRD  2
    
    State st = FIRST;    // Don't need the "enum" keyword here.
    But the enum is simpler. And you can create a typedef so you don't have to use the enum keyword to use the enum:
    Code:
    typedef enum State { FIRST, SECOND, THIRD } State;
    
    State st = FIRST;  // Don't need the enum keyword because of the typedef

  5. #5
    Registered User mc088's Avatar
    Join Date
    Jan 2017
    Posts
    19
    Thank you very much

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,707
    > Also, just wondering,not all C code are valid C++ code then? (just wondering, 'cuz apparently the my "c" code was compiled as "c++" .)
    No they're not.

    For example, you can say
    int class;
    in a C program, but C++ will barf. The same goes for any other C++ only keyword which would otherwise just be an identifier in C.

    They're not even functionally identical.
    Code:
    $ cat foo.c
    #include <stdio.h>
    
    int main(void)
    {
      printf("Char size=%zd\n", sizeof('a'));
      return 0;
    }
    $ gcc -Wall foo.c && ./a.out 
    Char size=4
    $ g++ -Wall foo.c && ./a.out 
    Char size=1
    Character constants in C++ really are chars, whereas they're ints in C.

    This old page shows the many differences between C99 and C++98.
    Incompatibilities Between ISO C and ISO C++
    There are yet further revisions to both languages, which increases the disparity.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enumeration
    By Mr.Lnx in forum C Programming
    Replies: 3
    Last Post: 06-11-2013, 10:08 AM
  2. What is enumeration
    By ncode in forum C Programming
    Replies: 3
    Last Post: 10-13-2011, 06:48 AM
  3. Enumeration help
    By CXHatchback in forum C Programming
    Replies: 3
    Last Post: 04-17-2003, 08:59 AM
  4. enumeration
    By GaPe in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2003, 06:11 AM
  5. enumeration
    By C-Struggler in forum C Programming
    Replies: 5
    Last Post: 03-13-2003, 09:36 AM

Tags for this Thread