Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-01-2006, 09:17 AM   #1 (permalink)
twinnie
Recruit
 
Join Date: Oct 2006
Posts: 6
twinnie is on a distinguished road
Grade program

I have tried to program a grade program but it keeps not printing out correctly
Please help


Code:
#include <stdio.h>  //standard library header file

int
 main( void)
{
	int grade;
	printf("Enter score number ");
	scanf("%i", &grade);
	if (grade < 0 || grade > 100)
	{
		printf("not valid");
	} 
	else if  (grade <= 0 && grade>= 59)
	{
		printf("F");
	 }
	else if (grade <= 60 && grade>= 69)
	{
		printf("D");
	}
	 else if  (grade >= 79 && grade<= 70)
	 {
		printf("C");
	 }
	  else if (grade <= 80 && grade>= 89)
	 {
		printf("B");
	 }
	else if (grade <= 90 && grade>= 100)
	{
		printf("A");
	 }
	  return 0;
}

Last edited by redhead; 11-01-2006 at 09:31 AM.
twinnie is offline   Reply With Quote
Old 11-01-2006, 09:32 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
redhead is on a distinguished road
Try:
Code:
scanf("%d", &grade);
And change your conditions to somethign usefull..
Code:
if  (grade <= 0 && grade>= 59)
How is tha possible ?? a grade which is less than 0 and at teh same time higher than 59 ???
You might want to change your testing conditions to somethign like:
Code:
if  (grade >= 0 && grade<= 59)
DO you see the difference now ??
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 11-01-2006, 09:54 AM   #3 (permalink)
twinnie
Recruit
 
Join Date: Oct 2006
Posts: 6
twinnie is on a distinguished road
I did change that but it keeps printing not valid
twinnie is offline   Reply With Quote
Old 11-01-2006, 10:19 AM   #4 (permalink)
toe_cutter
Code Monkey
 
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
toe_cutter is on a distinguished road
Send a message via ICQ to toe_cutter Send a message via AIM to toe_cutter Send a message via Yahoo to toe_cutter
Try changing this
Code:
if (grade < 0 || grade > 100)
to this

Code:
if ((grade < 0) || (grade > 100))
Sometimes it is the order of operation, and I'm not sure which gets calculated first? It is always better to seperate.

Toe
__________________
toe_cutter is offline   Reply With Quote
Old 11-01-2006, 10:52 AM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
redhead is on a distinguished road
then take a look at this:
Code:
#include <stdio.h>  //standard library header file

int main( void)
{
  int grade;
  printf("Enter score number ");
  scanf("%i", &grade);
  if (grade < 0 || grade > 100)
    {
      printf("not valid");
    } 
  else if  (grade >= 0 && grade<= 59)
    {
      printf("F");
    }
  else if (grade >= 60 && grade<= 69)
    {
		printf("D");
    }
  else if  (grade <= 79 && grade>= 70)
    {
      printf("C");
    }
  else if (grade >= 80 && grade<= 89)
    {
      printf("B");
    }
  else if (grade >= 90 && grade<= 100)
    {
      printf("A");
    }
  printf("\n");
  return 0;
}
All I did was change your testign conditions from ( <= && => ) to ( >= && =< ) plus I added a newline to be printed, so you'd see the actual grade on it's own line..

And it does work.....
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 11-01-2006, 11:43 AM   #6 (permalink)
twinnie
Recruit
 
Join Date: Oct 2006
Posts: 6
twinnie is on a distinguished road
Hey- Here is the function version. It does not work



#include <stdio.h> //standard library header file

char get_grades( double grade)
{

printf("Enter score number ");
scanf("%i", &grade);
if (grade < 0 || grade > 100)
{
return("not valid");
}
else if (grade >= 90 && grade<= 100)
{
return("A");
}
else if (grade >= 80 && grade<= 89)
{
return("B");
}
else if (grade <= 79 && grade>= 70)
{
return("C");
}
else if (grade >= 60 && grade<= 69)
{
return("D");
}
else if (grade >= 0 && grade<= 59)
{
return("F");
}

}/*Ends Function*/

int main( void)
{


get_grades(grade);
return 0;
}
twinnie is offline   Reply With Quote
Old 11-01-2006, 02:13 PM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Your function returns a char yet your function body returns const char* .

Thus return 'a' .
__________________
Valmont is offline   Reply With Quote
Old 11-01-2006, 02:55 PM   #8 (permalink)
twinnie
Recruit
 
Join Date: Oct 2006
Posts: 6
twinnie is on a distinguished road
return("B");

do you mean it should be

return('B');
twinnie is offline   Reply With Quote
Old 11-02-2006, 06:20 AM   #9 (permalink)
Deliverance
C++ Beginner
 
Join Date: Jul 2005
Location: Ottawa
Posts: 73
Deliverance is on a distinguished road
There's a couple things wrong with your function

1.
Code:
return("not valid");
Your function prototype is a char yet you're trying to return a string (char array I guess since it's C). Try just catching the error, and sending a message to stderr, followed by an exit. Other than that, once your function executes, you're not storing the result anywhere. you have to do something like this:
Code:
int main(void)
{
int grade; /* note you should have a value in here */
char a;
a = get_grades(grade); /* now we have your grade letter in here */
return 0;
}
The last thing to mention is this:
Remember, whenever you have an expression (if, while, for, else, etc)
it is only necessary to use { } if there is more than 1 statement to be executed for that comparison / expression, otherwise it will treat the next line as its body, either run or not, then go back to the regular program flow. since we're only doing a return call per 'if' comparison, don't bother with the braces.

Anyways, try something like this out for your function version, should work fine
Code:
#include <stdio.h> //standard library header file
#include <stdlib.h>

char get_grades( double grade)
{ 
	if (grade < 0 || grade > 100)
	{
		fprintf(stderr,"Unable to process grade...Invalid\n");
		exit(EXIT_FAILURE);
	}
	if (grade >= 90 && grade<= 100)
		return('A');
	else if (grade >= 80 && grade<= 89)
		return('B');
	else if (grade <= 79 && grade>= 70)
		return('C');	
	else if (grade >= 60 && grade<= 69)
		return('D');
	else return('F');
}/*Ends Function*/

int main(void)
{
	int grade;
	char letter;

	printf("Enter your score ");
	scanf("%i", &grade);

	letter = get_grades(grade);
	printf("Your %i%% is equal to a %c \n", grade, letter);
	return 0;
}
Deliverance is offline   Reply With Quote
Old 11-02-2006, 11:55 AM   #10 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
Originally Posted by twinnie View Post
return("B");

do you mean it should be

return('B');
Try it to find out .
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with copying backwards rogue Standard C, C++ 9 04-24-2005 04:39 PM
C++ Deadlock Detection Program Help... coolsc81 Standard C, C++ 2 10-26-2004 06:14 AM
Help on starting new program B00tleg Standard C, C++ 21 10-17-2004 12:58 PM


All times are GMT -8. The time now is 06:40 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting