dimanche 24 janvier 2016

Else if function in C not working as expected

My program is below, i'm trying to run it on visual studio (I hate visual studio) and it keeps giving me an error "Illegal if Without matching if". I believe it is trying to tell me that my else doesn't match my if, but it does. Below is my code, can someone run it and let me know what the problem is so i don't repeat it in the future

    /* counting number of students that passs*/
#include <stdio.h>

main()
{
    int pass, fail, grade;
    printf(" This program  tells you total number of students that passed\n Enter -1 to finish the program");

    pass = 0;
    fail = 0;
    grade = 0;

    while (grade != -1) {           /* Enter -1 to finish the while loop*/
        printf("Enter the grade of the student, 1 is pass, 2 is fail, -1 finishes the program\n");
        scanf_s("%d", &grade);
        if (grade == 1)
            printf("The student passed\n");
        pass = pass + 1;                /* Add 1 to the pass*/
        else if (grade == 2)
            printf("The student failed\n");
        fail = fail + 1;            /*Add 1 to fail */
        else
            printf("You have entered an invalid number, please try again\n");
    }

    if (pass > 8)
        printf("More than 8 students passed, raise tution fees");

    getchar();
}

Aucun commentaire:

Enregistrer un commentaire