-
The letters
O.k dudes I've tried just about everything to get this crap to work anyways. I just got through failing a test this week that basically asked us to enter a numerical value and return a letter grade using user written function . I $$$$ing hate character values. I never can seem to get them to return a letter. I got this instead
enter a grade
98
the grade you entered is ♦
enter a grade
here is my code
can someone please help me figure this out why is it not giving me a letter value and a box instead. here is the code
#include <stdio.h>
char getletgrade(char);
int main()
{
int d;
char total;
do
{
printf("enter a grade\n");
scanf("%d", &d);
printf("the grade you entered is %c\n", total);
}
while (total !=999);
return 0;
}
char getletgrade(char c)
{
char A, B, C, D;
int grade;
if
(grade > 89)
c = 'A';
else if
(grade > 80 && grade < 89)
c = 'B';
else if
(grade > 70 && grade < 79)
c = 'C';
else if
(grade > 60 && grade < 69)
c = 'D';
else if
(grade < 60)
c = 'F';
return (c);
}
-
First of all, read http://cboard.cprogramming.com/showt...threadid=25765
1. After that you have to actually call getletgrade() to give you back a value.
Something like:
Code:
printf("the grade you entered is %c\n", getletgrade(d));
2.You delcared "total" and never assign it a value, and you use it as a sentinel at the loop "while (total !=999);" which will have undefine behaviour since you did not initialise it's value as well.
You should check against "d" instead, that is where your user input variable is stored.
3. you should declare you protoype
Code:
char getletgrade(int);
since you're interest in comparing an integer value.
-
WOOPS.
My bad I was a bit hasty in my cutting and pasting here the actual code. The code is working now but all it gives me back no matter what number I enter is an F . WIERD!!!!!
#include <stdio.h>
char getletgrade(char);
int main()
{
int d;
char c;
do
{
printf("enter a grade\n");
scanf("%d", &d);
printf("the grade you entered is %c\n", getletgrade(c));
}
while (d !=999);
return 0;
}
char getletgrade(char c)
{
char A, B, C, D;
int grade;
if
(grade > 89)
c = 'A';
else if
(grade > 80 && grade < 89)
c = 'B';
else if
(grade > 70 && grade < 79)
c = 'C';
else if
(grade > 60 && grade < 69)
c = 'D';
else if
(grade < 60)
c = 'F';
return (c);
}
-
I guess you want to scan an int and return its letter?
Code:
#include <stdio.h>
char getletgrade(int);
int main()
{
int d;
for(; ;)
{
printf("enter a grade or 999 to quit\n");
scanf("%d%*c", &d);
if(d == 999) break;
printf("the grade you entered is %c\n", getletgrade(d));
}
return 0;
}
char getletgrade(int in)
{
char letter;
if(in >= 90)
letter = 'A';
else if(in >= 80)
letter = 'B';
else if(in >= 70)
letter = 'C';
else if(in >= 60)
letter = 'D';
else
letter = 'F';
return letter;
}
-
>>int grade;
>>if (grade > 89)
You didn't give grade a value before checking it. Like ronin showed, you're presumably supposed to be passing that value in to the function.
Read this for better methods of getting a number from the user.