Hi everyone, 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)) but it doesn't work on the char. on the char i can only do one: Code: if (digit2 > '5') Can't use the && for some reason. It doesn't create any compilation problem, but it doesn't work. I was wondering if someone can offer some insight on this. 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); }
so that s why you should write '1', '3' , ...'0' instead of 1,3...,0 operation like '1'<'3' is equal to same inequality for codes of '1' and '3' ( code of '1' is lower than code of '3') but there can be some other problems...
and 1 more operation '3'>4 is aviable it means code of char '3' ( 51) compares with 4 , result is true 51>4
aVague and DaWei, Thanks so much! Putting single quotes around the numbers took care of the problem. Definitely, a newbie mistake