'A "misplaced else" error in this C++ program

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


Solution 1:[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.

Solution 2:[2]

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

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Mortensen
Solution 2 Silvio Mayolo
Solution 3 user3750325