C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 4th, 2005, 10:38 AM
dekoi dekoi is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 12 dekoi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 27 m 52 sec
Reputation Power: 0
" parse error before `else' " **URGENT**

Please help me on this as soon as you can. Thank you so much.

Quote:
#include <stdio.h>

int
main(void)
{
double test1, test2, assign1, assign2, average;
int wholeaverage;
char grade;


printf("Enter mark for the first test: ");
scanf("%lf", &test1);

printf("\n");

printf("Enter mark for the second test: ");
scanf("%lf", &test2);

printf("\n");

printf("Enter mark for the first assignment: ");
scanf("%lf", &assign1);

printf("\n");

printf("Enter mark for the second assignment: ");
scanf("%lf", &assign2);

printf("\n");

average = (test1 + test2 + assign1 + assign2) / 4;

wholeaverage = average;

if(wholeaverage<=100 && wholeaverage>=80)
grade = 'A';

printf("Your grade is %c or %d.\n", grade, wholeaverage);

else if (wholeaverage<=79, wholeaverage>=70)
grade = 'B';

printf("Your grade is %c or %d.\n", grade, wholeaverage);

else if (wholeaverage<=69, wholeaverage>=60)
grade = 'C';

printf("Your grade is %c or %d.\n", grade, wholeaverage);

else if (wholeaverage<=59, wholeaverage>=50)
grade = 'D';

printf("Your grade is %c or %d.\n", grade, wholeaverage);

else if(wholeaverage<=49, wholeaverage>=0)
grade = 'F';

printf("Your grade is %c or %d.\n", grade, wholeaverage);

else
printf("The grade is undefined.\n");


if ((assign1<50 && assign2<50)||(assign1<50 || assign2<50))

if (grade == 'A');
printf("However, since you failed both/one of your assignments, your grade is reduced to B.");

else if (grade == 'B');
printf("However, since you failed both/one of your assignments, your grade is reduced to C.");

else if (grade == 'C');
printf("However, since you failed both/one of your assignments, your grade is reduced to D.");

else if (grade == 'D');
printf("However, since you failed both/one of your assignments, your grade is reduced to F.");

else
printf("However, since you failed both/one of your assignments, and your grade was initially F, the grade is undefined.");


else if ((test1<50 && test2<50) || (test1<50 || test2<50))

printf("Your average is F, since you failed one or both of your tests. \n");
printf("End of Program.\n");

else

printf("End of Program.\n");

return(0);

}


I receive ' parse error before `else' ' errors at basically every 'else' line in the program.

Thank you.

Reply With Quote
  #2  
Old October 4th, 2005, 10:47 AM
Icon's Avatar
Icon Icon is online now
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 740 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 12 h 33 m 16 sec
Reputation Power: 4
It should be something like this:

c Code:
Original - c Code
  1.  
  2. #include <stdio.h>
  3.  
  4. int main(void)
  5. {
  6.   double test1, test2, assign1, assign2, average;
  7.   int wholeaverage;
  8.   char grade;
  9.  
  10.  
  11.   printf("Enter mark for the first test: ");
  12.   scanf("%lf", &test1);
  13.  
  14.   printf("\n");
  15.  
  16.   printf("Enter mark for the second test: ");
  17.   scanf("%lf", &test2);
  18.  
  19.   printf("\n");
  20.  
  21.   printf("Enter mark for the first assignment: ");
  22.   scanf("%lf", &assign1);
  23.  
  24.   printf("\n");
  25.  
  26.   printf("Enter mark for the second assignment: ");
  27.   scanf("%lf", &assign2);
  28.  
  29.   printf("\n");
  30.  
  31.   average = (test1 + test2 + assign1 + assign2) / 4;
  32.  
  33.   wholeaverage = average;
  34.  
  35.   if(wholeaverage<=100 && wholeaverage>=80)
  36.   {
  37.     grade = 'A';
  38.  
  39.     printf("Your grade is %c or %d.\n", grade, wholeaverage);
  40.   }
  41.   else if (wholeaverage<=79, wholeaverage>=70)
  42.   {
  43.     grade = 'B';
  44.  
  45.     printf("Your grade is %c or %d.\n", grade, wholeaverage);
  46.   }
  47.   else if (wholeaverage<=69, wholeaverage>=60)
  48.   {
  49.     grade = 'C';
  50.  
  51.     printf("Your grade is %c or %d.\n", grade, wholeaverage);
  52.   }
  53.   else if (wholeaverage<=59, wholeaverage>=50)
  54.   {
  55.     grade = 'D';
  56.  
  57.     printf("Your grade is %c or %d.\n", grade, wholeaverage);
  58.   }
  59.   else if(wholeaverage<=49, wholeaverage>=0)
  60.   {
  61.     grade = 'F';
  62.  
  63.     printf("Your grade is %c or %d.\n", grade, wholeaverage);
  64.   }
  65.   else
  66.   {
  67.     printf("The grade is undefined.\n");
  68.   }
  69.  
  70.   if ((assign1<50 && assign2<50)||(assign1<50 || assign2<50))
  71.   {
  72.     if (grade == 'A')
  73.       printf("However, since you failed both/one of your assignments, your grade is reduced to B.");
  74.  
  75.     else if (grade == 'B')
  76.       printf("However, since you failed both/one of your assignments, your grade is reduced to C.");
  77.  
  78.     else if (grade == 'C')
  79.       printf("However, since you failed both/one of your assignments, your grade is reduced to D.");
  80.  
  81.     else if (grade == 'D')
  82.       printf("However, since you failed both/one of your assignments, your grade is reduced to F.");
  83.  
  84.     else
  85.       printf("However, since you failed both/one of your assignments, and your grade was initially F, the grade is undefined.");
  86.  
  87.   }
  88.   else if ((test1<50 && test2<50) || (test1<50 || test2<50))
  89.   {
  90.     printf("Your average is F, since you failed one or both of your tests. \n");
  91.     printf("End of Program.\n");
  92.   }
  93.   else
  94.   {
  95.     printf("End of Program.\n");
  96.   }
  97.  
  98.   return(0);
  99. }
  100.  


Please consult your programming book on how if-then-else statements work and especially pay some attention to the use of code blocks with the curly braces {}...
Comments on this post
Itsacon agrees: Damn, you beat me to it...

Reply With Quote
  #3  
Old October 4th, 2005, 10:49 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
Very simple, if you have an 'if' statement where you want more than one line of code to be conditional, you have to enclose them in braces { }.
Right now, only the first line of code after each if() statement is attached to the condition. The next line is normal code, and as such, the following 'else' can does not belong to any 'if'.
Try this:
cpp Code:
Original - cpp Code
  1. #include <stdio.h>
  2.  
  3. int
  4. main(void)
  5. {
  6.     double test1, test2, assign1, assign2, average;
  7.     int wholeaverage;
  8.     char grade;
  9.  
  10.  
  11.     printf("Enter mark for the first test: ");
  12.     scanf("%lf", &test1);
  13.  
  14.     printf("\n");
  15.  
  16.     printf("Enter mark for the second test: ");
  17.     scanf("%lf", &test2);
  18.  
  19.     printf("\n");
  20.  
  21.     printf("Enter mark for the first assignment: ");
  22.     scanf("%lf", &assign1);
  23.  
  24.     printf("\n");
  25.  
  26.     printf("Enter mark for the second assignment: ");
  27.     scanf("%lf", &assign2);
  28.  
  29.     printf("\n");
  30.  
  31.     average = (test1 + test2 + assign1 + assign2) / 4;
  32.  
  33.     wholeaverage = average;
  34.  
  35.     if(wholeaverage<=100 && wholeaverage>=80)      // here you have more than one line in each 'if' statement, so you need braces
  36.     {
  37.         grade = 'A';
  38.  
  39.         printf("Your grade is %c or %d.\n", grade, wholeaverage);
  40.     }
  41.     else if (wholeaverage<=79, wholeaverage>=70)
  42.     {
  43.         grade = 'B';
  44.  
  45.         printf("Your grade is %c or %d.\n", grade, wholeaverage);
  46.     }
  47.     else if (wholeaverage<=69, wholeaverage>=60)
  48.     {
  49.         grade = 'C';
  50.  
  51.         printf("Your grade is %c or %d.\n", grade, wholeaverage);
  52.     }
  53.     else if (wholeaverage<=59, wholeaverage>=50)
  54.     {
  55.         grade = 'D';
  56.  
  57.         printf("Your grade is %c or %d.\n", grade, wholeaverage);
  58.     }
  59.     else if(wholeaverage<=49, wholeaverage>=0)
  60.     {
  61.         grade = 'F';
  62.  
  63.         printf("Your grade is %c or %d.\n", grade, wholeaverage);
  64.     }
  65.     else
  66.     {
  67.         printf("The grade is undefined.\n");
  68.     }
  69. // here every 'if' statement only has one line of code, so you don't need braces.
  70.     if ((assign1<50 && assign2<50)||(assign1<50 || assign2<50))
  71.     {
  72.        
  73.         if (grade == 'A');
  74.             printf("However, since you failed both/one of your assignments, your grade is reduced to B.");
  75.         else if (grade == 'B');
  76.             printf("However, since you failed both/one of your assignments, your grade is reduced to C.");
  77.         else if (grade == 'C');
  78.             printf("However, since you failed both/one of your assignments, your grade is reduced to D.");
  79.         else if (grade == 'D');
  80.             printf("However, since you failed both/one of your assignments, your grade is reduced to F.");
  81.         else
  82.             printf("However, since you failed both/one of your assignments, and your grade was initially F, the grade is undefined.");
  83.     }
  84.     else if ((test1<50 && test2<50) || (test1<50 || test2<50))
  85.     {
  86.         printf("Your average is F, since you failed one or both of your tests. \n");
  87.         printf("End of Program.\n");
  88.     }
  89.     else
  90.     {
  91.         printf("End of Program.\n");
  92.     }
  93.  
  94.     return(0);
  95.  
  96. }
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 280

Last edited by Itsacon : October 4th, 2005 at 11:09 AM.

Reply With Quote