switch case statement

This is a discussion on switch case statement within the C++ Programming forums, part of the General Programming Boards category; I'm getting errors with the following switch case statement. Code: /*switch (command_read) { case command_list[0]: { printf("Hello\n"); } case command_list[1]: ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    switch case statement

    I'm getting errors with the following switch case statement.

    Code:
    /*switch (command_read)
    		{
    			case command_list[0]:
    			{
    				printf("Hello\n");
    			}
    			case command_list[1]:
    			{
    				printf("NHL\n");
    			}
    			case command_list[2]:
    			{
    				printf("NFL\n");
    			}
    			case command_list[3]:
    			{
    				printf("Kingston\n");
    			}
    			case command_list[4]:
    			{
    				printf("Eraser\n");
    			}
    		}*/
    I'm getting the following error:
    --------------------Configuration: IO - Win32 Debug--------------------
    Compiling...
    IO.CPP
    G:\Current Courses\EECE 314\projects\el_with_synchronization\IO\IO.CPP(71) : error C2051: case expression not constant
    G:\Current Courses\EECE 314\projects\el_with_synchronization\IO\IO.CPP(73) : error C2051: case expression not constant
    G:\Current Courses\EECE 314\projects\el_with_synchronization\IO\IO.CPP(75) : error C2051: case expression not constant
    G:\Current Courses\EECE 314\projects\el_with_synchronization\IO\IO.CPP(77) : error C2051: case expression not constant
    G:\Current Courses\EECE 314\projects\el_with_synchronization\IO\IO.CPP(79) : error C2051: case expression not constant
    G:\Current Courses\EECE 314\projects\el_with_synchronization\IO\IO.CPP(81) : warning C4060: switch statement contains no 'case' or 'default' labels
    Error executing cl.exe.

    IO.exe - 5 error(s), 1 warning(s)
    I'm not entirely sure about what's wrong with the implementation using the array indices. Any help would be much appreciated.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    case labels should be constants
    use enum for example
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, the case-label needs to be an integer constant that the compiler knows at the compile-time. You are using an indexed array, which the compiler can't make into a constant at the time of compilation.

    Vart suggests an enum, which is one good choice, e.g. :
    Code:
    enum somename {Value1, Value2, Value3};
    
       somename val;
    ...
       switch(val)
       {
           case Value1:
                   ...
           case Value2:
                   ...
           case Value3:
                   ...
           default:
                   ...
        }
    If you have an array that contains some values, and you really want to switch on the content of the array, then you may want to use a loop to find the correct index and then switch on the index. Or you could make a two-element array:
    Code:
    struct st {
       int val;
       char *str;
    };
    
    static const st command_list[] =  { 
       { 0, "Hello" }, 
       { 1, "NHL" },
       { 2, "NFL" },
       ...
    };
    
    ...
       for(i = 0; i < sizeof(command_list) / sizeof(command_list[0]); i++)
       {
           if (command_list[i].val == command_read) 
           {
               printf("%s\n", command_list[i].str);
               break;
           }
        }

    There may be other solutions that are better for what you want to do, but without understanding what you are actually trying to achieve, this is the best suggestions I can give.
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Don't forget to put break on each case, otherwise it will continue to the next one.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  2. Strings and Switch Statement
    By Ajsan in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2004, 02:16 PM
  3. Extra printed stmts...why?
    By mangoz in forum C Programming
    Replies: 4
    Last Post: 12-19-2001, 07:56 AM
  4. A simple array question
    By frenchfry164 in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2001, 04:13 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 03:47 PM

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