I'm a beginner working through Oualline's Practical C Programming, and one of the exercises (6-3) is to wite a program that will report a letter grade, including + or -, given a numeric grade.
The problem I encountered is that the char comparisons are not working as I expected. Of course, I'm a newbie, so I probably shouldn't expect anything! But in any case, it's not like the regular int comparisons. On the int, I could do this:
Code:
if ((0 <= percent) && (60 >= percent))
Code:
if (digit2 > '5')
The reason I'm using a char, is because I want the second digit of the inputted numeric grade. Depending on what it is, the program will append, a +, a - or nothing.
I've included the whole program below. The problematic section is the one marked with the comment "not working right now."
Any help would be appreciated. I look forward to learning from you.
Rick
Code:
/*****************************************
* Exercise 6-3 p.93
* Given a numeric grade, print the letter
* 05/06/2007 16:00:47
* *****************************************/
#include <stdio.h>
char line[4]; /* standard input -- enough for 3 numbers */
int percent; /* inputted numeric grade */
int digit2; /* second digit of input */
char plus; /* holds +, - or empty space char */
int main() {
/* print prompt and get the input */
printf("Welcome to the grade calculator\n\n");
printf("Input a numeric grade: ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%d", &percent);
sscanf(line, "%c%c", &digit2, &digit2); /* second assignment overwrites the first; char for int seems okay */
/* set variable plus to +, - or neither */
/* not working right now 05/06/2007 17:07:44 */
if ((1 <= digit2) && (3 >= digit2))
plus = '-';
else if ((4 <= digit2) && (7 >= digit2))
plus = ' ';
else if ((8 <= digit2) && (0 >= digit2))
plus = '+';
else
plus = ' ';
/* start debugging */
if (digit2 > '5') {
plus = '+';
printf("%c Greater than 5 %c ", plus, plus);
}
printf("%c", digit2);
printf("%c", plus);
/* end debugging */
/* see where the input falls */
if ((0 <= percent) && (60 >= percent))
printf("F%c", plus);
else if ((61 <= percent) && (70 >= percent))
printf("D%c", plus);
else if ((71 <= percent) && (80 >= percent))
printf("C%c", plus);
else if ((81 <= percent) && (90 >= percent))
printf("B%c", plus);
else if ((91 <= percent) && (100 >= percent))
printf("A%c", plus);
else
printf("You entered an invalid percentage value.\n");
return(0);
}

