Skip to content Skip to sidebar Skip to footer

Caesarc228 Error Expected Identifier or Char Continue

  1. #1

    cabisheka is offline

    Registered User


    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; }
    Last edited by Salem; 03-24-2018 at 06:09 AM. Reason: Code added by Salem


  2. #2

    laserlight is offline

    C++ Witch laserlight's Avatar


    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

    Salem is offline

    and the hat of int overfl Salem's Avatar


    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; }


  4. #4

    christophergray is offline

    Registered User


    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

    ordak is offline

    Registered User


    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.


heidemanaborecturs.blogspot.com

Source: https://cboard.cprogramming.com/c-programming/176011-error-expected-identifier-before-else.html

Enregistrer un commentaire for "Caesarc228 Error Expected Identifier or Char Continue"