my program below, i'm trying run on visual studio , keeps giving me error illegal if without matching if
.
i believe trying tell me else
doesn't match if
, does. below code; can run , let me know problem don't repeat in future?
/* counting number of students pass*/ #include <stdio.h> main() { int pass, fail, grade; printf(" program tells total number of students passed\n enter -1 finish program"); pass = 0; fail = 0; grade = 0; while (grade != -1) { /* enter -1 finish while loop*/ printf("enter grade of student, 1 pass, 2 fail, -1 finishes program\n"); scanf_s("%d", &grade); if (grade == 1) printf("the student passed\n"); pass = pass + 1; /* add 1 pass*/ else if (grade == 2) printf("the student failed\n"); fail = fail + 1; /*add 1 fail */ else printf("you have entered invalid number, please try again\n"); } if (pass > 8) printf("more 8 students passed; raise tuition fees\n"); getchar(); }
braces friends. change snippet
if (grade == 1) printf("the student passed\n"); pass = pass + 1; /* add 1 pass*/ else if (grade == 2) printf("the student failed\n"); fail = fail + 1;
to
if (grade == 1){ printf("the student passed\n"); pass = pass + 1; /* add 1 pass*/ } else if (grade == 2){ printf("the student failed\n"); fail = fail + 1; }
Comments
Post a Comment