Help with value returning function.

Afternoon guys, so I am having an issue with my calcGPA function. It is not returning any value other then 0, and im not really sure why. It is probably something I am overlooking, I could use a hint. Thanks
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
  #include<iostream>
#include<string>

using namespace std;


double calcGPA(int classNum, int credit[], char grade[]);



int main()
{
	int classNum;
	cout <<"How many courses did you take this semester?" << endl;
    cin >> classNum;
    string courseName[10];
    char grade[10];
    int credit[10];

    for(int i =0; i< classNum; i++)
	 {
	       cout <<"Please enter the course name." << endl;
	       cin >> courseName[i];
	       cout <<"Please enter the courses grade and credit amount.";
	       cin >> grade[i] >> credit[i];
	       
	   }
   calcGPA(classNum, credit, grade);
   cout <<"Your GPA is :" << calcGPA(classNum,credit,grade);
   system("pause");
   return 0;
}
double calcGPA(int classNum, int credit[], char grade[])
{
	double qualityPoint = 0;
	double creditHours = 0;
	double count = 0;
	for(int i=0; i< classNum; i++)
	{
		creditHours = credit[i] + creditHours;
	}
	
	for(int i=0; i>=classNum; i++)
	{
		switch(grade[i])
		{
			case 'A': count = credit[i] * 4.00 + count;
			break;
			
			case 'B': count = credit[i] *3.00 + count;
			break;
			
			case 'C': count = credit[i] * 2.00 + count;
			break;
			
			case 'D': count = credit[i] *1.00 + count;
			break;
			
			case 'F': count = credit[i] *0.00 + count;
			break;
			
			default : cout << "Invalid grade" << endl;
                     
			}
			
		}
		qualityPoint = count;
		
		return qualityPoint * creditHours;
	}
It's because your switch statement is never entered, therefore count is always equal to 0, therefore qualityPoint will be zero, therefore qualityPoint * creditHours will be 0.

The reason is your for-loop - for(int i=0; i>=classNum; i++)

Specifically the i >= classNum. Im sure you can see your mistake here.
Change it to - for(int i=0; i < classNum; i++)
Last edited on
Thanks, its always something simple.
You should learn how to debug so you can catch them in a minute instead of an hour. It's pretty important^^
Topic archived. No new replies allowed.