GPA calculation(need help)

Oct 12, 2014 at 4:11am
I am new here and having trouble with my program at the moment. What I got so far is not doing the calculation. what i need the program to do is for the use to type in the course name and the grade, and calculate the grade to get the GPA. Is there an easy way to play with the array or which way would be better for me to approach this?

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
using namespace std;

struct GPA
{
	string class1;
	long long total;
};

int main()
{


	int n;
	int answer;
	int gpa;
	char choice1;
	char grade;
	int A = 4;
	int B = 3;
	int C = 2;
	int D = 1;
	int F = 0;
	int a = 0;



		cout << "Would you like to enter a grade: Y or N ";
		cin >> choice1;
	
		if (choice1 == 'Y')
		{
			cout << "How many courses are we inputing: ";
			cin >> n;

			GPA * pointer = new GPA[n];

			while (a < n)
			{
				for (int i = 0; i < n; i++)
				{
					cout << "Please enter the course name: ";
					cin >> pointer[i].class1;

					cout << "Please enter the grade in caps: ";
					cin >> grade;

					if (grade = 'A')
					{
						pointer[i].total = A;
					}
					if (grade = 'B')
					{
						pointer[i].total = B;
					}
					if (grade = 'C')
					{
						pointer[i].total = C;
					}
					if (grade = 'C')
					{
						pointer[i].total = C;
					}
					if (grade = 'D')
					{
						pointer[i].total = D;
					}
					if (grade = 'F')
					{
						pointer[i].total = F;

					}
				}
				a++;
			}

			for (int k = 0; k < n; k++)
			{
				answer = pointer[k].total + 0;
			}
			gpa = answer / n;
			cout << "Your GPA is: " << gpa << endl;
		}
		if (choice1 == 'N')
		{
			cout << "Program is closing... " << endl;
		}
		
			return 0;
			
		}
Oct 12, 2014 at 4:46am
For one, try using a switch statement.

Two, if you don't use a switch statement, use if-else. Forgetting the else is a bad programming habit.

Three, if you use if's and comparisons, don't forget the second equal sign. Your entire if-block assigns values to grade, instead of doing anything meaningful.
Oct 12, 2014 at 6:41am
do you have to use a class as a requirement?
or you are just trying it out?
Oct 12, 2014 at 10:21pm
I tried a different way of doing the GPA by using the switch statement. A lot easier to use. what stopping me right now is adding up the inputs of the grades and getting the GPA. How would I get the inputs into an array or some type of storage and then calculate the GPA out?



the class is just there. still want it as an input and that's it.
Oct 13, 2014 at 12:07am
WOW... i made it harder then it needed too. figured it out. thanks guys for some of the tips.
Topic archived. No new replies allowed.