Thread: switch case error in program

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    switch case error in program

    What's mistake in my program I just want to do it , if some one enter specific color then print specific statement for each color

    Code:
    #include <stdio.h>
    int main(void)
    {
     
        unsigned char favorite_color;
     
        printf("Please choose your favorite color: ");
        scanf("%d", &favorite_color);
     
        /* print out the result */
        switch (favorite_color)
        {
        case red:
            printf("your favorite color is Red");
            break;
        case green:
            printf("your favorite color is Green");
            break;
        case blue:
            printf("your favorite color is Blue");
            break;
        default:
            printf("you did not choose any color");
        }
     
        return 0;
    }
    output
    hello.c: In function 'main':
    hello.c:14:10: error: 'red' undeclared (first use in this function)
    case red:
    ^~~
    hello.c:14:10: note: each undeclared identifier is reported only once for each function it appears in
    hello.c:17:10: error: 'green' undeclared (first use in this function)
    case green:
    ^~~~~
    hello.c:20:10: error: 'blue' undeclared (first use in this function)
    case blue:
    ^~~~

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    The compiler tells exactly what the problem is - you provided some identifiers (red, green, blue) which were not previously declared. switch-case instruction handles only integer values, so you need to make those colour names integers, e.g.:
    Code:
    enum color
    {
        red = 1,
        green = 2,
        blue = 3
    };
    You should also display those values so the user could choose from them.
    Code:
    printf("%d - red\n", red);
    printf("%d - green\n", green);
    printf("%d - blue\n", blue);
    Last edited by DRK; 01-08-2018 at 02:15 AM. Reason: typo

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by DRK View Post
    The compiler tells exactly what the problem is - you provided some identifiers (red, green, blue) which were not previously declared. switch-case instruction handles only integer values, so you need to make those colour names integers, e.g.:
    I understand switch case with enum . Actually I didn't want to use enum. I just wanted to make program where I will use only switch case statement. can you tell me how to make it without enum
    Last edited by vead; 01-08-2018 at 02:58 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,986
    Perhaps
    #define red 0

    Or
    const int red = 0;

    Or
    case 0:
    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. Need explanation with switch case program!
    By lovecoder in forum C Programming
    Replies: 2
    Last Post: 08-07-2017, 02:53 AM
  2. Replies: 4
    Last Post: 11-19-2016, 08:24 PM
  3. Switch-case program
    By saros3578 in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2015, 09:17 AM
  4. Help with functions & switch case in my converting program
    By tlxxxsracer in forum C Programming
    Replies: 3
    Last Post: 04-20-2015, 08:16 AM
  5. switch case error. Can anyone help?
    By brack in forum C Programming
    Replies: 4
    Last Post: 02-28-2011, 03:30 PM

Tags for this Thread