Thread: error: expected identifier or '(' before 'else'

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    1

    error: expected identifier or '(' before 'else'

    I wrote a basic c program with if and else if statements to calculate a hotel bill but I keep getting the same error.
    long.c:97:8: error: expected identifier or ‘(’ before ‘else’
    else if(room == 3)
    ^~~~
    long.c:133:2: error: expected identifier or ‘(’ before ‘else’
    else
    ^~~~
    long.c:136:9: error: expected identifier or ‘(’ before ‘return’
    return 0;
    ^~~~~~
    long.c:137:1: error: expected identifier or ‘(’ before ‘}’ token

    The program has been attached,


    Code:
    #include<stdio.h>
    int main(void)
    {
    	//declaring variables
    	float total;
    	char card, accbasis;
    	int room, days;
    
    
    	//printing statements to get inputs
    	printf("Enter type of room:\n");
    	scanf("%d", &room);
    	
    	printf("Enter accomodation basis:\n");
            scanf("%c ", &accbasis);
    
    	printf("Enter card type:\n");
            scanf("%c ", &card);
    		
    	printf("Enter number of days:\n");
            scanf("%d", &days);
    
    
    	//conditions
    	if(room == 1)
    	{
    		if(accbasis == 'F')
    		{
    			if(card == 'G')
    				total = 25555.00 * days - 25555.00 * 12.5;
    			else if(card == 'S')
    				total = 25555.00 * days - 25555.00 * 11.5;
    			else if(card =='B')
    				total = 25555.00 * days - 25555.00 * 9.5;
    			else
                             {
                                    printf("Invalid card type.");
                                    printf("The total is %.2f", total);
                             }
    		}	
    		else if(accbasis == 'H')
    		{
    			if(card == 'G')
                                    total = 17250.00 * days - 17250.00 * 12.5;
                            else if(card == 'S')
                                    total = 17250.00 * days - 17250.00 * 11.5;
                            else if(card == 'B')
                                    total = 17250.00 * days - 17250.00 * 9.5;
    			else 
    			{
                                    printf("Invalid card type.");       
                                    printf("The total is %.2f" , total);
                             }
    
    
    		}
    		else
    			printf("Invalid accomodation basis.");
    	
    	}	
    	else if(room == 2)
            {
                   		 if(accbasis == 'F')
    		{
                            	if(card == 'G')
                                    	total = 17500.00 * days - 17500.00 * 12.5;
                            	else if(card == 'S')
                                    	total = 17500.00 * days - 17500.00 * 11.5;
                            	else if(card == 'B')
                                    	total = 17500.00 * days - 17500.00 * 9.5;
    				else
    			{
                            	        printf("Invalid card type.");  
                                    	printf("The total is %.2f", total);
                             }
    
    
    		}
                    
    	}	
    		else if(accbasis == 'H')
                    { 		if(card == 'G')
                     	               	total = 12250.00 * days - 12250.00 * 12.5;
                            	 else if(card == 'S')
                                    	total = 12250.00 * days - 12250.00 * 11.5;
                             	else if(card == 'B')
                                    	total = 12250.00 * days - 12250.00 * 9.5;
            		 	else
    			{
    				printf("Invalid card type");
    				printf("The total is %.2f", total);
    			}
    		}
    		else
    			printf("Invalid accomodation basis");
    	}
           else if(room == 3)
            {
            
    
           		 if(accbasis == 'F')
    		{
                            	if(card == 'G')
                                    	total = 9000.00 * days - 9000.00 * 12.5;
                            	else if(card == 'S')
                                    	total = 9000.00 * days - 9000.00 * 11.5;
                            	else if(card == 'B')
                                    	total = 9000.00 * days - 9000.00 * 9.5;
    				else
                                         {
                                                 printf("Invalid card type.");
                                                 printf("The total is %.2f", total);
                                         }
    
                    }
    			else if(accbasis == 'H')
    		{
                            	 if(card == 'G')
                                    	total = 7250.00 * days - 7250.00 * 12.5;
                             	else if(card =='S')
                                    	total = 7250.00 * days - 7250.00 * 11.5;
                             	else if(card == 'B')
                                    	total = 7250.00 * days - 7250.00 * 9.5;
    			 	else
    				{
    					printf("Invalid card type.");
    			       	        printf("The total is %.2f", total);
    				}
    		}
    
           }
    	
    	else
    		printf("Invalid room type");
    	
            return 0;
    }
    Attached Files Attached Files
    Last edited by Salem; 03-24-2018 at 06:09 AM. Reason: Code added by Salem

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,749
    For some reason I cannot seem to download the file you uploaded. I suggest that you post, in [code][/code] bbcode tags, the smallest and simplest version of your program that you expect should compile but which demonstrates this error.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,997
    Well the basic reason is because your crappy indentation means you've no idea which braces match up.

    You have too many closing braces.

    The one on line 96 matches the start of main.

    So everything after that is bad code.

    Code:
    #include<stdio.h>
    int main(void)
    {
      //declaring variables
      float total;
      char card, accbasis;
      int room, days;
    
      //printing statements to get inputs
      printf("Enter type of room:\n");
      scanf("%d", &room);
    
      printf("Enter accomodation basis:\n");
      scanf("%c ", &accbasis);
    
      printf("Enter card type:\n");
      scanf("%c ", &card);
    
      printf("Enter number of days:\n");
      scanf("%d", &days);
    
      //conditions
      if (room == 1) {
        if (accbasis == 'F') {
          if (card == 'G')
            total = 25555.00 * days - 25555.00 * 12.5;
          else if (card == 'S')
            total = 25555.00 * days - 25555.00 * 11.5;
          else if (card == 'B')
            total = 25555.00 * days - 25555.00 * 9.5;
          else {
            printf("Invalid card type.");
            printf("The total is %.2f", total);
          }
        } else if (accbasis == 'H') {
          if (card == 'G')
            total = 17250.00 * days - 17250.00 * 12.5;
          else if (card == 'S')
            total = 17250.00 * days - 17250.00 * 11.5;
          else if (card == 'B')
            total = 17250.00 * days - 17250.00 * 9.5;
          else {
            printf("Invalid card type.");
            printf("The total is %.2f", total);
          }
        } else
          printf("Invalid accomodation basis.");
      } else if (room == 2) {
        if (accbasis == 'F') {
          if (card == 'G')
            total = 17500.00 * days - 17500.00 * 12.5;
          else if (card == 'S')
            total = 17500.00 * days - 17500.00 * 11.5;
          else if (card == 'B')
            total = 17500.00 * days - 17500.00 * 9.5;
          else {
            printf("Invalid card type.");
            printf("The total is %.2f", total);
          }
        }
        /*!! this is your actual bad brace } */
        else if (accbasis == 'H') {
          if (card == 'G')
            total = 12250.00 * days - 12250.00 * 12.5;
          else if (card == 'S')
            total = 12250.00 * days - 12250.00 * 11.5;
          else if (card == 'B')
            total = 12250.00 * days - 12250.00 * 9.5;
          else {
            printf("Invalid card type");
            printf("The total is %.2f", total);
          }
        } else
          printf("Invalid accomodation basis");
      } else if (room == 3) {
        if (accbasis == 'F') {
          if (card == 'G')
            total = 9000.00 * days - 9000.00 * 12.5;
          else if (card == 'S')
            total = 9000.00 * days - 9000.00 * 11.5;
          else if (card == 'B')
            total = 9000.00 * days - 9000.00 * 9.5;
          else {
            printf("Invalid card type.");
            printf("The total is %.2f", total);
          }
        } else if (accbasis == 'H') {
          if (card == 'G')
            total = 7250.00 * days - 7250.00 * 12.5;
          else if (card == 'S')
            total = 7250.00 * days - 7250.00 * 11.5;
          else if (card == 'B')
            total = 7250.00 * days - 7250.00 * 9.5;
          else {
            printf("Invalid card type.");
            printf("The total is %.2f", total);
          }
        }
      }
      else
        printf("Invalid room type");
    
      return 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.

  4. #4
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Code editors can reformat your code with good indentation (except for nested ifs in Python, but not a problem in C or other languages with braces.) Also, on GNU systems there is a stand alone indent program for C called indent, but it is probably harder to configure to your preferred style than your editor is (and indents default style is weird.)

  5. #5
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    101
    Quote Originally Posted by christophergray View Post
    Code editors can reformat your code with good indentation (except for nested ifs in Python, but not a problem in C or other languages with braces.) Also, on GNU systems there is a stand alone indent program for C called indent, but it is probably harder to configure to your preferred style than your editor is (and indents default style is weird.)
    In Code::Blocks IDE you can select some code, right click, Format use AStyle.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 08-22-2015, 07:44 PM
  2. Replies: 11
    Last Post: 04-14-2013, 11:15 AM
  3. Replies: 3
    Last Post: 08-21-2012, 11:50 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