Why it does not work

This is a discussion on Why it does not work within the C++ Programming forums, part of the General Programming Boards category; Hi All, Can anybody explain why the code below does not work? enum State {NONE, OFF, ON, RUN, STOP}; int ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    85

    Question Why it does not work

    Hi All,

    Can anybody explain why the code below does not
    work?

    enum State {NONE, OFF, ON, RUN, STOP};

    int StateArr[5];
    State S;

    for (S = NONE; S<=STOP; S++)
    StateArr[S] = 10;

    How can I fix the code?
    Thanx

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Well, as far as I know the enum command sets up a number of constants with integer values. Thus, when you set up your for loop, you are trying to increment a constant; you might as well say "2++". Try the following:

    [code]
    enum State {NONE, OFF, ON, RUN, STOP};

    int StateArr[ 5 ];

    for (int i = NONE; i <= STOP; i++)
    StateArr[ i ] = 10;
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    85
    Originally posted by geophyzzer
    Well, as far as I know the enum command sets up a number of constants with integer values. Thus, when you set up your for loop, you are trying to increment a constant; you might as well say "2++". Try the following:

    [code]
    enum State {NONE, OFF, ON, RUN, STOP};

    int StateArr[ 5 ];

    for (int i = NONE; i <= STOP; i++)
    StateArr[ i ] = 10;

    What do you mean?
    Since S is a variable of State type? So S is
    a constant??? Are you sure about this??
    Code:
     State S;
     for (S = NONE; S<= STOP; S++)
           StateArr[S] = 10;
    This is perfectly work in C, but not in C++ and
    I don't know why it has compile error.
    DV007

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    In C++, you are not allowed to increment enums. In C, you are allowed to do so.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >In C++, you are not allowed to increment enums.
    Well, you can...but it gets ugly. I think there was a thread I answered about this a while back with reasons, a workaround, and alternatives.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    If I may, (taken from The C++ Programming Language; Bjarne Stroustrup):

    Code:
    enum Day { sun, mon, tue, wed, thu, fri, sat };
    
    Day& operator++ (Day& d)
    {
         return d = (sat==d) ? sun : Day(d+1);
    }
    (Very likely the explanation, in part, that Prelude had presented in the earlier post she refers to.)

    Note that, if the operator function is not a member, at least one argument must be passed to the operator function.

    Interestingly, the operator function presented would seem to imply that enum's can, be incremented, i.e. Day(d+1), albeit with a limitation. The compiler-recognized value cannot be incremented for a specific member, but we can refer to another member of the enumeration by adding (subtracting?) a constant value from it.

    Or, is this a misinterpretation on my part? (If I had a quarter...)

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 08:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 11:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 05:36 PM

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