Thread: Switch Case error: expected identifier before 'R'

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    90

    Switch Case error: expected identifier before 'R'

    Compiler show error expected identifier before 'R'

    Code:
    #include<stdio.h>
    
    
    
    int main (void)
    
    
    {
    	enum colour {'R', 'G', 'Y'}TrafficLight;
    	
    	scanf("%c", TrafficLight);
    	
    	switch(TrafficLight)
    	{
    		case 'R': 
    		        printf("Red Light\n");
    				break;
    		case 'G':  
    		       printf("Green Light \n");
    			   break;
    		case 'Y':  
    		       printf("Yellow Light\n");
    			   break;
            default:
                   printf("Light OFF \n");				   
                   break;			   
    	}
    	
    	return 0;
    	
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You probably just want to declare TrafficLight as a char here and not bother with the enum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    90
    Quote Originally Posted by laserlight View Post
    You probably just want to declare TrafficLight as a char here and not bother with the enum.
    Program only except integer value so why it doesn't except character input

    Code:
    #include<stdio.h>
    
    int main (void)
    
    
    {
    	enum colour {R, G, Y}TrafficLight;
    	
    	scanf("%d", TrafficLight);
    	
    	switch(TrafficLight)
    	{
    		case R: 
    		        printf("Red Light\n");
    				break;
    		case G:  
    		       printf("Green Light \n");
    			   break;
    		case Y:  
    		       printf("Yellow Light\n");
    			   break;
            default:
                   printf("Light OFF \n");				   
                   break;			   
    	}
    	
    	return 0;
    	
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expected Identifier Error
    By maxx96 in forum C Programming
    Replies: 4
    Last Post: 11-24-2018, 08:29 AM
  2. error: expected identifier or '(' before 'else'
    By cabisheka in forum C Programming
    Replies: 4
    Last Post: 04-02-2018, 10:00 AM
  3. Replies: 7
    Last Post: 08-22-2015, 07:44 PM
  4. error: expected identifier or β(β before βwhileβ
    By philgrek in forum C Programming
    Replies: 9
    Last Post: 04-19-2011, 01:28 PM
  5. Error: expected identifier or ‘(’ before ‘{’ token
    By jpcanaverde in forum C Programming
    Replies: 66
    Last Post: 06-08-2010, 12:53 PM

Tags for this Thread