First off yes this is for school. I am using a basic program I already have that takes 5 numbers and averages them. I am trying to design a basic program that asks for name (easy), number of grades to calculate, then I input the grades in letter format; a, b, c, d, f. I am having two big problems. I can not seem to figure out how to control the number of grades that pop up. I am starting to get pretty confused as I am unfortunently not the best programmer.

The second problem is I am quite lost on how to convert the letter grade to the corresponding number (4,3,2 etc). I am pretty sure it involves a switch statement... past that, I don't know how to use them well.

Code so far (haven't touched the 2nd half, first I am just trying to control how many grade inputs pop up). Can't get it to paste, so I attached it. Any suggestions on a better direction?

Recommended Answers

All 7 Replies

Check this link

Humm... I am still a bit confused on things, I am still trying to figure out how to control the number of grades that pop up. Good link though, thanks!

Ahhhhh!!!! I am about ready to give up! I can not get the name to work. I can compile it and I seem to be able to store the name by using Scanf and two %s %s, problem is I can not recall it! I get some integer pointer error. I am beyond frustruated right now. PLEASE help out if you can. STILL unable to past code in here so I will once again attach the newest version. I got the grade numbers sorted now I just need to display name at the end (and take it in properlly at the start) and convert numbers to grades.

Cheers.

I allready said that you read the link. Instead of scanf use fgets. This is your code.

/* This program asks the user for a few numbers and computes their average */

#include <stdio.h>
#include <string.h>
#define NumNumbers 5



int main( int argc, char** argv )
{
	int	i;
	int	g;
	float	number;
	float	average;
	char	fName[255];
	char	lName[255];
	
	
	
	printf( "Enter the name: ");
	scanf( " %s %s", fName, lName);
	
	printf( "Enter the number of grades to average: ");
	scanf( " %d", &g );
	
	printf( "Enter %d grades (must be one of A, B, C, D, F)...\n", g );
	average = 0.0;
	for( i = 0; i <= g-1 ; i++ )
	{
		do
		{
			printf( "  Grade %d: ", i+1 );
			/* getchar(); */
			scanf( " %f", &number );
			
			if( (number < 0.0) || (number > 10.0) )
				printf( "   Invalid grade (%g)\n", number );
		}
		while( (number < 0.0) || (number > 10.0) );
		
		average += number;
	}
	average /= g;

	/* Print the results */
	printf( "has a GPA of %.2f\n", average);
	
	return( 0 );
}

For temp solution uncomment the getchar, but you must use fgets instead of scanf. After that you dont need the getchar kludge.

Thank you again, I got that part to work, however I am having trouble storing the name. It worked fine with two %s's for storing but I can not seem to get it to print the name (Kyle Davidson) for example. It gives me an error.

Thank you again, I got that part to work, however I am having trouble storing the name. It worked fine with two %s's for storing but I can not seem to get it to print the name (Kyle Davidson) for example. It gives me an error.

Did you try something like this?

char * ch = NULL;
	
	printf( "Enter the first name: ");
	fgets(fName, sizeof(fName), stdin);
	if ((ch = strchr(fName, '\n')) != NULL)
	   *ch = '\0';
	printf( "Enter the last name: ");
	fgets(lName, sizeof(lName), stdin);
	if ((ch = strchr(lName, '\n')) != NULL)
	   *ch = '\0';
	printf("%s %s\n", fName, lName);

How would I ask for the name all together the prompt has to be. The program once compiled and ran must look like this.

Enter the name: Kyle Davidson
Enter the number of grades to average: 5
Enter 5 grades (must be one of A, B, C, D, F)...
Grade 1: A
Grade 2: b
Grade 3: z
Invalid grade (z)
Grade 3: C
Grade 4: d
Grade 5: e
Invalid grade (e)
Grade 5: a
Kyle Davidson has a GPA of 2.80

Cheers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.