Nested IF else statement in C

Viewed 64

I can't seem to locate the problem in my program where it says, "error: expected declaration or statement at the end of input"

I would like to learn more about this mistake

#include<stdio.h>

int main()
{
    float Math, English, Science, Fundamentals, per;
    
    printf("Enter GPA of 4 subject\n");
    scanf("%f %f %f %f", &Math, &English, &Science, &Fundamentals);
    
    per = (Math + English + Science + Fundamentals) / 4.0;
    
    if(per >= 5)
    {
        printf("Your GWA is: 75\n");
    }
    else
    {
        if(per >= 3)
        {    
            printf("Your GWA is: 75\n");
        }
        else
        {
            if(per >= 2)
            {
                printf("Your GWA is: 85\n");
            }
            else
            {
                if(per >= 1)
                {
                      printf("Your GWA is: 100\n");
                }
            }
        }        
        
     return 0;
    }
1 Answers

First of all, you have 8 opening braces but only 7 closing braces.

If we assume, that the return statement should be the last statement in your code, then the end of your code should look like this:

    } // end last else
    return 0;
} // end main

And, have you ever heard of the else if construct:

     if (per >= 5) {...}
else if (per >= 3) {...}
else if (per >= 2) {...}
else if (per >= 1) {...}
else               {...}
Related