-3

I cannot seem to fix the "misplaced else" error in the code below. This code should collect and compute for term grades and give remarks depending on the score.

#include <conio.h>
#include <stdio.h>

main()
{
    char name[20];
    int exam, q1, q2, q3, ass, sw, att, avgq, CS, TG;
    clrscr();
    printf("Name: ");
    gets(name);
    printf("\nExam: ");
    scanf("%d", &exam);
    printf("\nQuiz #1: ");
    scanf("%d", &q1);
    printf("\nQuiz #2: );
    scanf("%d", &q2);
    printf("\nQuiz #3: ");
    scanf("%d", &q3);
    printf("\nAssignment: ");
    scanf("%d", &ass);
    printf("\nSeatwotk: ");
    scanf("%d", &sw);
    printf("\nAttendance: ");
    scanf("%d", &att);
    CS = (0.4*ass) + (0.4*sw) + (0.2*att);  // class standing //
    avgq = (q1 + q2 + q3)/3;  // Average quiz //
    TG = (0.4*exam) + (0.3*avgq) + (0.3*CS);  // Term grade //
    if(TG >= 90)
       printf("Term Grade: %d", TG);
       printf("Remarks: EXCELLENT");
    else if (TG>=80 && TG<=89)
       printf("Term Grade: %d", TG);
       printf("Remarks: SATISFACTORY");
    else if (TG>=76 && TG<=79)
       printf("Term Grade: %d", TG);
       printf("Remarks: GOOD");
    else if (TG == 75)
       printf("Term Grade: %d", TG);
       printf("Remarks: PASSING");
    else if (TG<74)
       printf("Term Grade: %d", TG);
       printf("Remarks: FAILED");
    else
       printf("Invalid Input.  Try again");
    getch();
    return 0;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mon Cabrera
  • 1
  • 1
  • 1
  • 1
  • 5
    You need to add braces `{}` for each of your `if` blocks. Your code is horribly broken without them. C++ doesn't scope based on indentation like Python. ***NEVER OMIT CURLY BRACES FROM YOUR CODE.*** – Carcigenicate Jul 29 '17 at 23:38
  • 2
    You 're on c++ not python, use curly brackets. – DimChtz Jul 29 '17 at 23:39
  • This is a mega duplicate. What is the canonical question? – Peter Mortensen May 13 '22 at 16:06
  • A candidate (2013—though not canonical (there must be some from 2008 or 2009)): *[Why do I get a compiling error that says error: ‘else’ without a previous ‘if’?](https://stackoverflow.com/questions/17587875/)* – Peter Mortensen May 13 '22 at 16:11
  • Related (about mitigation. Canonical): *[What's the purpose of using braces (i.e. {}) for a single-line if or loop?](https://stackoverflow.com/questions/12193170/)* – Peter Mortensen May 13 '22 at 16:13

3 Answers3

1

Uh oh! Noob alert! Just kidding; we all have to start somewhere ;)

So do not worry, fair maiden! The problem lies here:

When you declare an if statement, you must encompass the body of the if statement with curly braces. If you don't do that, only the first line below the if statement will be run. Here's an example:

// Here, both do something 1 and do something 2 are being run in the 'if' statement
if (something) {
    do something 1;
    do something 2;
}

// Here, only do something 1 will get run inside the 'if' statement
if (something)
    do something 1;
    do something 2;

So back to your problem. You must put curly braces {} around the code in an if statement if the if statement body consists of more than one line.

if (something)
    do something 1;
    do something 2;
else
    do something 3;

is equivalent to

if (something)
    do something 1;
do something 2;
else do something 3;

That is why your else statement throws an error. Each else must have an if before it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
travisjayday
  • 784
  • 6
  • 16
0

C++ doesn't use indentation to determine the ends of statements. You need braces if you want more than one statement.

Instead of:

if (a)
  b;
  c;
else
  d;

Use:

if (a) {
  b;
  c;
} else {
  d;
}
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
0

If you have more than 1 line under an if or else, they must be contained in curly braces, like this:

if(TG>=90)
{
   printf("Term Grade: %d",TG);
   printf("Remarks: EXCELLENT");
}
else if (TG>=80 && TG<=89)
{
   printf("Term Grade: %d",TG);
   printf("Remarks: SATISFACTORY");
}
user3750325
  • 1,502
  • 1
  • 18
  • 37