Thread: Enumeration question

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    39

    Enumeration question

    Just working with some enumerations here
    in a switch.

    I thought we could use a switch as long as it was a constant and an enumeration evalutes to a constant. I keep getting a compile error here.
    Im sure there are other ways this can be done but my requirements make me do it as it shows below.

    Can anyone explain this compile error?
    Thanks

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    
    typedef enum {NORTH,SOUTH,EAST,WEST} direction;
    
    
    
    int main()
    {
    
    int x=0;
    int y=0;
    int i;
    
    
    
    switch (direction)
    {
    
    case NORTH:
        y++;
        break;
    
    case SOUTH:
        y--;
        break;
    
    case EAST:
        x++;
        break;
    
    case WEST:
        x--;
        break;
    
    default: x=x,y=y;
    
    }
    
    
    
    
    return 0;
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > switch (direction)
    direction is the name of the type, not the name of a variable.

    You need
    direction whereto = NORTH;
    switch ( whereto )
    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.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    Works now!
    Thanks!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enumeration question
    By Link_26 in forum C++ Programming
    Replies: 1
    Last Post: 05-25-2011, 10:11 AM
  2. Enumeration Question
    By Marth_01 in forum C Programming
    Replies: 14
    Last Post: 11-06-2008, 11:00 AM
  3. enumeration design question
    By l2u in forum C++ Programming
    Replies: 17
    Last Post: 04-14-2008, 09:35 AM
  4. enumeration question
    By jerrykelleyjr in forum C++ Programming
    Replies: 14
    Last Post: 12-17-2004, 11:59 AM
  5. Enumeration question?
    By cheesehead in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2001, 06:31 PM