C2181 illegal else without matching if

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void) {
float Grade;
char A;
char B;
char C;
char D;
char F;

puts("Enter a student Grade");
scanf("%f", &Grade);
if (Grade <= 59.9) {

printf("The Grade is F ", Grade);
}
else if (Grade >= 60 && Grade <= 69.9) {

printf("The Grade is D", Grade);

}
else if (Grade >= 70 && Grade <= 79.9) {

printf("The Grade is C", Grade);

}
else if (Grade >= 80 && Grade <= 89.9) {

printf("The Grade is B", Grade);

}
else if (Grade >= 90 && Grade <= 100) {

printf("The Grade is A", Grade);

}



return 0;
}



am getting two errors one is illegal else without matching if on line 24 and Error C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNING on line 8 can someone see what is the problem that i have here please
Last edited on
I quickly copied and pasted your code into my editor, there's nothing wrong with it. Also, please use code tags when posting code.
Last edited on
It seems Error C4996 is Microsoft related - if you changed compiler, it wouldn’t give you that error.
To avoid that error, someone suggests using the following instructions before loading your header:
1
2
3
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif 


I couldn’t find the cause of your “else without if" error, because my compiler didn’t complain about anything, as well as goldenchicken’s one. Anyway also the following code compiles without errors or warnings, but I can’t know if it’s what you aimed to do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifdef _MSC_VER
    #define _CRT_SECURE_NO_WARNINGS
#endif // _MSC_VER

#include <stdio.h>

int main()
{
    double Grade;
    puts("Enter a student Grade: ");
    scanf("%lf", &Grade);

    if (Grade <= 59.9) {
        printf("The Grade %d means a F", int(Grade));
    }

    else if (Grade >= 60 && Grade <= 69.9) {
        printf("The Grade %d means a D", int(Grade));
    }

    else if (Grade >= 70 && Grade <= 79.9) {
        printf("The Grade %d means a C", int(Grade));
    }

    else if (Grade >= 80 && Grade <= 89.9) {
        printf("The Grade %d means a B", int(Grade));
    }

    else if (Grade >= 90 && Grade <= 100) {
        printf("The Grade %d means a A", int(Grade));
    }
}


However in C++ you shouldn’t use headers like <stdio.h>, but their equivalent like <cstdio>.
Are you programming in C, perhaps? Maybe you want to post your question in a C forum.
warning C4996: 'scanf': This function or variable may be unsafe.
It's not an error, but a warning. It can be ignored or disabled .
However the other warnings should be fixed.
printf("The Grade is F ", Grade);
warning C4474: 'printf' : too many arguments passed for format string
1
2
3
4
5
char A;
char B;
char C;
char D;
char F;

warning C4101: 'A': unreferenced local variable
warning C4101: 'B': unreferenced local variable
warning C4101: 'C': unreferenced local variable
warning C4101: 'D': unreferenced local variable
warning C4101: 'F': unreferenced local variable


thank you all for your help
Topic archived. No new replies allowed.