GPA program : giving big negative value

Hello !

I am very, very new to programming and one of our assignments was to make a GPA program with two functions (not allowed to use arrays). If anyone could be so kind as to tell me why my program is giving me a huge negative number and to tell me where I went wrong I would really appreciate it <:)

Oh! Also sorry I forgot to mention but if you enter that you only want to calculate the GPA for one class it immediately gives an error that it can't divide by zero but I'm not sure how it's even dividing by zero??

PS: this assignment was already turned in so don't worry about giving me homework answers or something x)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include  <iostream>
#include  <iomanip>
using namespace std;

int numericGrade, classWorth, subtotal, units = 0;
char  grade;
int get_points(int);
int get_totals(int& total_points, int& num, int& points, int& total);
int main()

{
	int num;
	int total_points;
	int points;
	int total;
	cout <<"\nHow many classes would you like to calculate GPA for?";
	cin >> num;
	get_totals(total_points, num,points,total);


	system("pause");
	return 0;
}

int get_points(int points)
{

	if (grade == 'A' || grade == 'a')
	
		return points = 4;
	
	if (grade == 'B' || grade == 'b')
	
		return points = 3;
	
	if (grade == 'C' || grade == 'c')
	
		return points = 2;

	if (grade == 'D' || grade == 'd')
	
		return points = 1;
	
	if (grade == 'F' || grade == 'f')
	
		return points = 0;
	
	return points;

}

int get_totals(int& total_points, int& num, int& points, int& total)
{


	int GPA;
	for (int i = 0; i < num; i++)
	{
		cout << "What letter grade did you get in class " << ' ' << i << ":" << endl;
		cin >> grade;
		numericGrade = get_points(points);
		cout << "How many units was class" << ' ' << i << ' ' << "worth?" << endl;
		cin >> classWorth;
		subtotal = numericGrade  * classWorth;
		total = total + subtotal;
		units = classWorth;
		units = units + classWorth;
	}
	
	GPA = total / units;
	cout <<"Your GPA is:" << setprecision(2) << GPA << endl;
	return GPA;
}
Last edited on
line 65 - total hasn't been initialized, so it has some random garbage value in it when you add it to subtotal.

line 61 - don't you mean to send the letter grade just entered on line 60 to the get_points function instead of points which is uninitialized when you send it in to the function to evaluate?

line 66-67 - you're ending up with double the value of classWorth in units here. Not sure if that's what you meant to do?
Last edited on
Thank you for replying! I will try and fix these problems!
line 30 etc. - you can just return 4;

and you might want to add validation that they've entered a valid letter grade somewhere.


Edit: Is there some particular reason you are using global variables in this particular program? It's a bit confusing when some are global and others are local when they're all fairly closely related.
Last edited on
so I fixed everything you told me to fix and I'm getting actual numbers now (thank you!)

but I'm not really sure how to fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int get_totals(int& total_points, int& num, int& points, int total)
{

	int GPA;
	for (int i = 0; i < num; i++)
	{
		cout << "What letter grade did you get in class " << ' ' << i << ":" << endl;
		cin >> grade;
		numericGrade = get_points(grade);
		cout << "How many units was class" << ' ' << i << ' ' << "worth?" << endl;
		cin >> classWorth;
		subtotal = numericGrade  * classWorth;
		total = total + subtotal;
		units = classWorth + classWorth;
		
	}
	
	GPA = total / units;
	cout <<"Your GPA is:" << GPA << endl;
	return GPA;
}


It's giving me an extra unit but then how do I make it so that with each new units that are added it adds it to the total?
It sounds like you want to do something like what you've done with line 13 to increment the total with the currently calculated total. You have points and total_points variables - did you mean to have these work like a subtotal and total?
Yeah, exactly and I thought that was the correct way to go about it but it's not working.

Annnd yes, the total_points and points variables were supposed to work like total and subtotal but I never managed to get that to work so I just changed the approach (and I forgot to delete them, sorry about that!)
oh wait, thank you so much for pointing out that out, I just did exactly the same thing I did with line 13 to the line 14. Thank you so much, you were a huge help!!!
Topic archived. No new replies allowed.