Thread: Grade program
View Single Post
Old 11-02-2006, 07: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