Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <stdio.h>

//function to convert letter grade into a number
float GradePoints(char grade)
{
	float gP;

	switch (grade)
	{
	case 'A':
	case 'a':
		gP = 4.0;
		break;

	case 'B':
	case 'b':
		gP = 3.0;
		break;

	case 'C':
	case 'c':
		gP = 2.0;
		break;

	case 'D':
	case 'd':
		gP = 1.0;
		break;

	case 'F':
	case 'f':
		gP = 0.0;
		break;
	}

	return gP;
}


//function to calculate grade point average
float Gpa(char grades[], char hours[], int arrLength)
{
	//gradepoint * time in class
	float totalGpHours = 0;
	//time in class
	float totalHours = 0;
	//final grade point average 
	float gradePointAverage;
	int i;

	for (i = 0; i < arrLength; ++i)
	{
		//add the user input time in each class to get total time in class. this is the denominator
		totalHours = hours[i] + totalHours;
		//multiplies hours in class and number grade from that class and adds the product to totalGpHours. this is the numerator
		totalGpHours = totalGpHours + (GradePoints(grades[i]) * hours[i]);

	}

	//calculate grade point average
	gradePointAverage = totalGpHours / totalHours;

	return gradePointAverage;

}



int main()
{
	//add one more element to the array because of the null
	char grades[6];
	int classTime[5];
	int i;

	//Ask user for letter grades
	printf("Please enter the letter grades you received (upper-case letters, no spaces):\n");
	//gets input plus one for the array grades
	fgets(grades, 6, stdin);

	//Ask user for time in each class
	printf("Please enter the time you spent in each course(in hours)");

	for (i = 0; i < 5; ++i)
	{
		//add one to i so it looks normal for the user and you don't start at course 0
		printf("\n Course %d: ", i + 1);
		scanf("%d", &classTime[i]);
	}


	printf("\n");
	printf("Your grade point average is: %f\n\n", Gpa(grades, classTime, 5));


	getchar();

	return 0;
}


What I have tried:

everything. if i got 4 the first cycle and 4 the next, it's supposed to add them, but it doesn't.
Posted
Updated 27-Oct-16 17:21pm
v2

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
I will show you the relevant lines:
float Gpa(char grades[], char hours[], int arrLength)
// ...
char grades[6];
int classTime[5];
// ...
Gpa(grades, classTime, 5)

Did you see it now?
The Gpa function expects three parameters of type char[], char[], and int. But you are passing char[], int[], and int.

The compiler should recognize this too and throw out a warning or error. If you did not got such compiler message, increase the warning level of the compiler (e.g. by using -Wall with GCC and /Wall with MS compilers).

To solve this change your Gpa function to
float Gpa(char grades[], int hours[], int arrLength)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900