Like the title says, it only output the first if statement. I had tried making the include format generalAverage < 97 && generalAverage >94
and adding .00
to the conditions but nothing worked.
#include<stdio.h>
char remarks[7][20] = {"Excellent", "Very Good", "Good", "Satisfactory", "Fair", "Passed", "Failed"}, opt;
float prelim, midterm, final, generalAverage, equivalent;
int main(){
do {
printf("STUDENT GRADE CALCULATOR\n");
printf(" Enter the Prelim Grade : ");
scanf(" %f", &prelim);
printf(" Enter the Midterm Grade : ");
scanf(" %f", &midterm);
printf(" Enter the Final Grade : ");
scanf(" %f", &final);
printf("\n");
generalAverage = (prelim midterm final) / 3;
if (generalAverage > 96) {
printf(" The General Average is : %.2f\n", generalAverage);
printf(" Equivalent : 1.0\n");
printf(" Remarks : %s\n", remarks[0]);
}
else if (generalAverage < 97) {
printf(" The General Average is : %.2f\n", generalAverage);
printf(" Equivalent : 1.25\n");
printf(" Remarks : %s\n", remarks[0]);
}
else if (generalAverage < 94) {
printf(" The General Average is : %.2f\n", generalAverage);
printf(" Equivalent : 1.50\n");
printf(" Remarks : %s\n", remarks[1]);
}
else if (generalAverage < 89) {
printf(" The General Average is : %.2f\n", generalAverage);
printf(" Equivalent : 1.75\n");
printf(" Remarks : %s\n", remarks[1]);
}
else if (generalAverage < 88) {
printf(" The General Average is : %.2f\n", generalAverage);
printf(" Equivalent : 2.00\n");
printf(" Remarks : %s\n", remarks[2]);
}
else if (generalAverage < 85) {
printf(" The General Average is : %.2f\n", generalAverage);
printf(" Equivalent : 2.25\n");
printf(" Remarks : %s\n", remarks[2]);
}
else if (generalAverage < 82) {
printf(" The General Average is : %.2f\n", generalAverage);
printf(" Equivalent : 2.50\n");
printf(" Remarks : %s\n", remarks[3]);
}
else if (generalAverage < 77) {
printf(" The General Average is : %.2f\n", generalAverage);
printf(" Equivalent : 2.75\n");
printf(" Remarks : %s\n", remarks[4]);
}
else if (generalAverage == 75) {
printf(" The General Average is : %.2f\n", generalAverage);
printf(" Equivalent : 3.00\n");
printf(" Remarks : %s\n", remarks[5]);
}
else if (generalAverage < 75) {
printf(" The General Average is : %.2f\n", generalAverage);
printf(" Equivalent : 5.00\n");
printf(" Remarks : %s\n", remarks[6]);
}
printf("\n");
printf("Do you want to continue (Type y or n): ");
scanf(" %c", &opt);
}while(opt == 'Y'||opt == 'y');
}
CodePudding user response:
First if{} and second else if{ }block considers all the cases.
CodePudding user response:
Your else if are in the wrong order.
if the generalAverage is 80 it will always go into the first else if becourse its < 97.
just start with the smalest and work your way up.
CodePudding user response:
You should check from "tight", via "more generous" to "else".
if (generalAverage < 75) {
}
else if (generalAverage == 75) {
}
else if (generalAverage < 77) {
}
else if (generalAverage < 82) {
}
else if (generalAverage < 85) {
}
else if (generalAverage < 88) {
}
else if (generalAverage < 89) {
}
else if (generalAverage < 94) {
}
else if (generalAverage < 97) {
} else {
}
If you start with (or early on have) generous checks like
if (generalAverage < 97)
all the rest is ignored, because of the (otherwise sensible) if-else chain.