|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
First time programmer here. I need some help on this C program I'm writing. I want to make a program that can take numerical grades and output both the numerical grade and letter grade, and can also loop and let me enter more if I press y for yes.
Two problems with what I've got so far. First of all, when I put it a numerical grade it always outputs the letter grade as 70. Why is this? Also, when I press y to enter another grade, it takes me back to the "Do you want to enter another grade?" statement, instead of taking me back to the grade entry statement. How did I mess that up? Thanks for your help, the code is contained below: #include<stdio.h> #pragma warning(disable:4996) int getNumGrade (); int calcLetterGrade(); int main (void) { int numgrade = 0; int lettergrade = 0; char cgoAgain = 'y'; printf ("Enter numerical grade value now:"); numgrade = getNumGrade (); lettergrade = calcLetterGrade(); printf ("\n When numerical grade is %d,",numgrade); printf ("letter grade is %d.", lettergrade); while (cgoAgain =='y' || cgoAgain == 'Y') { printf("\nDo you want to enter another grade?"); scanf("%c%*c",&cgoAgain); } return 0; } int getNumGrade() { int numgrade = 0; scanf("%d%*c",&numgrade); return numgrade; } int calcLetterGrade (numgrade) { int lettergrade; if (numgrade < 60) lettergrade = 'F'; else if (numgrade <70) lettergrade = 'D'; else if(numgrade < 80) lettergrade = 'C'; else if (numgrade <90) lettergrade = 'B'; else if (numgrade <100) lettergrade = 'A'; return lettergrade; } |
|
#2
|
|||
|
|||
|
Quote:
I am not sure about the repeating the input process but your 'lettergrade' is defined as "int" in your function and main program...it should be a 'char' |
|
#3
|
|||
|
|||
|
Quote:
Shouldn't matter really as chars are simply 8-bit integers and integers can be assigned single letters. But casting or declaring as char may be a good idea for both clarity and future compatability aswell as memory consumption but in this case the memory consumption is to minimal to even matter. I'm going to assume that the reason it's always 70 is because you're not supplying a parameter to the function, not sure how this compiled, but this: Code:
numgrade = getNumGrade (); lettergrade = calcLetterGrade(); should probably be this: Code:
numgrade = getNumGrade (); lettergrade = calcLetterGrade(numgrade); You also will have to change the function declaration to: int calcLetterGrade(int numgrade) At both the top and at the actual function definition. As for the looping issue its simply because the only code inside the loop is the 2 lines asking if they want to input another grade, the loop needs to encompass the whole body. Change it to this: Code:
int main (void)
{
int numgrade = 0;
int lettergrade = 0;
char cgoAgain = 'y';
while (cgoAgain =='y' || cgoAgain == 'Y')
{
printf ("Enter numerical grade value now:");
numgrade = getNumGrade ();
lettergrade = calcLetterGrade(numgrade);
printf ("\n When numerical grade is %d,",numgrade);
printf ("letter grade is %d.", lettergrade);
printf("\nDo you want to enter another grade?");
scanf("%c%*c",&cgoAgain);
}
return 0;
}
Hope that helps. |
![]() |
| Viewing: ASP Free Forums > Other > Programming Help > C Program Help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|