letter-grade.c: Change if-else chain to switch statement

This commit is contained in:
BBaoVanC 2020-10-20 20:54:24 -05:00
parent 2beb50bb25
commit b45e3670cc
No known key found for this signature in database
GPG Key ID: 6D74C8B0E7D791C2

View File

@ -7,17 +7,27 @@ int main() {
if ((input >= 0) && (input <= 100)) { if ((input >= 0) && (input <= 100)) {
char letter; char letter;
if ((input/10) >= 9) { int inputRange = input/10;
switch(inputRange) {
case 10:
case 9:
letter = 'A'; letter = 'A';
} else if ((input/10) >= 8) { break;
case 8:
letter = 'B'; letter = 'B';
} else if ((input/10) >= 7) { break;
case 7:
letter = 'C'; letter = 'C';
} else if ((input/10) >= 6) { break;
case 6:
letter = 'D'; letter = 'D';
} else { break;
case 5:
default:
letter = 'F'; letter = 'F';
} }
printf("Your letter grade is %c\n", letter); printf("Your letter grade is %c\n", letter);
} else { } else {
printf("ERROR: Input must be no more than 100 and no less than 0\n"); printf("ERROR: Input must be no more than 100 and no less than 0\n");