As a beginner's programming exercise I wrote this "letter grade calculator":
which looks like this on the console:Code:#include <stdio.h> char line[100]; /* line of data for input */ int grade; /* numeric grade */ int main() { while (1) { printf("Enter the numeric grade\n"); printf(" or 0 to stop:"); fflush(stdout); fgets(line, sizeof(line), stdin); sscanf(line, "%d", &grade); if (grade == 0) break; else if (grade > 0) if (grade <= 60) printf("F\n"); else if (grade <= 70) printf("D\n"); else if (grade <= 80) printf("C\n"); else if (grade <= 90) printf("B\n"); else if (grade <= 100) printf("A\n"); } return (0); }
Enter the numeric grade
or 0 to stop:66
D
Enter the numeric grade
or 0 to stop:77
C
Enter the numeric grade
or 0 to stop:33
F
Enter the numeric grade
or 0 to stop:88
B
Enter the numeric grade
or 0 to stop:100
A
Enter the numeric grade
or 0 to stop:81
B
Enter the numeric grade
or 0 to stop:0
Critiques, please! Is there a way to double up the comparison operators around the variable to test each range?



LinkBack URL
About LinkBacks



. Most of us around here don't like it when you ask a question without trying to answer it yourself
. People are more likely to respond when you do.
Seems a bit strange, why have ABCDF, and no E.