Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsOtherProgramming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old June 2nd, 2008, 06:30 PM
tristangreer tristangreer is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 1 tristangreer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 53 sec
Reputation Power: 0
Question C Program Help

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;
}

Reply With Quote
  #2  
Old June 3rd, 2008, 10:08 AM
nik707 nik707 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 25 nik707 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 39 m 19 sec
Reputation Power: 0
Quote:
Originally Posted by tristangreer
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;
}


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'

Reply With Quote
  #3  
Old June 23rd, 2008, 08:35 AM
denn0069 denn0069 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 9 denn0069 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 16 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by nik707
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'

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.

Reply With Quote
Reply

Viewing: ASP Free ForumsOtherProgramming Help > C Program Help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway