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 offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 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: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
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: 1,029 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: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 41 m 23 sec
Reputation Power: 15
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: 592

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

Reply With Quote
  #4  
Old October 4th, 2005, 10:56 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 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: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
In the second part of his code he has only one line for every if so he does not need braces but he does need to remove the semi-colon ; after the if statement I think.

Reply With Quote
  #5  
Old October 4th, 2005, 11:08 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: 1,029 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: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 41 m 23 sec
Reputation Power: 15
Send a message via ICQ to Itsacon
Indeed he does, failed to spot that one.

However, I made some other alterations anyway:
See the comments for the explanation:
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.     // You can't just assign a double to an integer, you need to typecast it, like this:
  34.     wholeaverage = (int)average;
  35.  
  36.     // using a switch is usually prefered over many consecutive elseif() statements
  37.     // here I use an integer division to easily divide the grade into portions.
  38.     // I also take the printf out of the loop, as it's the same for every condition.
  39.     switch(wholeaverage / 10)
  40.     {
  41.         case 10:
  42.         case 9:
  43.         case 8:
  44.             grade = 'A';
  45.             break;
  46.         case 7:
  47.             grade = 'B';
  48.             break;
  49.         case 6:
  50.             grade = 'C';
  51.             break;
  52.         case 5:
  53.             grade = 'D';
  54.             break;
  55.         default:
  56.             grade = 'F';
  57.     }
  58.     printf("Your grade is %c or %d.\n", grade, wholeaverage);
  59.  
  60.  
  61. //  if ((assign1<50 && assign2<50)||(assign1<50 || assign2<50))
  62. //  The above if statement is overly complex. If the first option evaluates to true, the second will too, so you can reduce it to just the second one:
  63.     if (assign1<50 || assign2<50)
  64.     {
  65.         // here we could again use a switch() statement, but since these are we are talking characters, it can be done even neater:
  66. /*    if (grade == 'A');
  67.             printf("However, since you failed both/one of your assignments, your grade is reduced to B.");
  68.         else if (grade == 'B');
  69.             printf("However, since you failed both/one of your assignments, your grade is reduced to C.");
  70.         else if (grade == 'C');
  71.             printf("However, since you failed both/one of your assignments, your grade is reduced to D.");
  72.         else if (grade == 'D');
  73.             printf("However, since you failed both/one of your assignments, your grade is reduced to F.");
  74.         else
  75.             printf("However, since you failed both/one of your assignments, and your grade was initially F, the grade is undefined."); */
  76.  
  77.         if(grade == 'F')
  78.             printf("However, since you failed both/one of your assignments, and your grade was initially F, the grade is undefined."); */
  79.         else
  80.             printf("However, since you failed both/one of your assignments, your grade is reduced to %c.", ++grade);       
  81.         // you see, since chars are essentially values between 0 and 255, we can just increase them by one to get the next one.
  82.  
  83.  
  84.     }
  85. //  else if ((test1<50 && test2<50) || (test1<50 || test2<50))    // here the same applies, only the 'OR' statement is enough
  86.     else if (test1<50 || test2<50)
  87.     {
  88.         printf("Your average is F, since you failed one or both of your tests. \n");
  89.         printf("End of Program.\n");
  90.     }
  91.     else
  92.     {
  93.         printf("End of Program.\n");
  94.     }
  95.  
  96.     return(0);
  97.  
  98. }


What I also note, is that if somebody fails an assignment AND a test, he only gets a reduced grade, not an F, so it might be wise to interchange these two sections, so that failing a test takes precedence over failing an assignment.

Hope this helps.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > " parse error before `else' " **URGENT**


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap