Enumeration question

This is a discussion on Enumeration question within the C Programming forums, part of the General Programming Boards category; Just working with some enumerations here in a switch. I thought we could use a switch as long as it ...

  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
    32,854
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 11: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, 10: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

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