Caesarc228 Error Expected Identifier or Char Continue
-
03-23-2018 #1
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 '}' tokenThe 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
-
03-24-2018 #2
C++ Witch
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.
Originally Posted by Bjarne Stroustrup (2000-10-14)
Look up a C++ Reference and learn How To Ask Questions The Smart Way
-
03-24-2018 #3
and the hat of int overfl
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; }
-
04-01-2018 #4
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.)
-
04-02-2018 #5
Registered User
Originally Posted by christophergray
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"