Array function grade book problem

So this compiles but when it runs it exits before executing the getAvg function and doesn't execute anything after the getAvg function call. I have been looking at this for hours, what am I missing that is causing this to happen?

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
void getNames (string[], int);
void getScores (string[], double[], int);
double getAvg (double[], int);	// average function prototype

int main()
{
	const int NAMES = 5;	  // Number of names to hold.
	const int GRADES = 5;	  // Number of grades to hold.
	const int SCORES = 4;  // Number of scores to hold for each student.
	string name[NAMES];		  // Five string array for names.
	char grade[GRADES]; 	  // Five char array for grades.
	double sScores[SCORES];	 // Four double array to hold student scores.
	int count; // Loop counter
	double average; // To hold average.
	
	// Get five student names from user.
	getNames(name, NAMES);
	
	// Get four scores for each student from user.
	getScores(name, sScores, SCORES);
	
	// Get each students average.
	average = getAvg(sScores, SCORES);
	
	for (count = 0; count < NAMES; count++)
	{
		cout << "The grade average for " << name[count] << " is: " 
			 << average << endl;
	}
	
	//	Make sure we place the end message on a new line
    cout << endl;

	//	The following is system dependent.  It will only work on Windows
    system("PAUSE");

	/* 
	// A non-system dependent method is below
	cout << "Press any key to continue";
	cin.get();
	*/
    return 0;
}
void getNames(string name[], int totNames)
{
	int count; // Loop counter
	for (count = 0; count < totNames; count++)
	{
		cout << "\nEnter the name of student " << (count + 1) << ": ";
		cin >> name[count];
	}
}
void getScores(string name[], double scores[], int totScores)
{
	int num; // Loop counter
	for (num = 0; num < totScores; num++)
	{
		// Accept scores 1-100 only.
		while (scores[num] < 0 || scores[num] > 100)
		{
			cout << "Error: Enter scores in range 1-100: ";
			cin >> scores[num];
		}
		cout << "Enter score " << (num + 1) <<" for " << name[num] << ": ";
		cin >> scores[num];
	}
}
double getAvg(double scores[], int scoreTotal)
{
	int count;  // Loop counter
	double avg, sum;
	avg = sum / scoreTotal;
	for (count = 0; count < scoreTotal; count++)
		sum += scores[count];
	return avg;
}
For one, you don't need to declare a counter for a loop outside the loop itself. Just put the declaration where you put the assignment to the initial value. As for the issue here, look at line 72. What value does the variable sum hold right there?
I fixed the loop declarations, and set sum = 0 so the getAvg functions looks like this:
double getAvg(double scores[], int scoreTotal)
{
double avg,
sum = 0;
avg = sum / scoreTotal;
for (int count = 0; count < scoreTotal; count++)
sum += scores[count];
return avg;
}

I still get exited out before the function runs.
Well, for one put
avg = sum / scoretotal;
after the actual for loop, so the function makes sense and doesn't just return 0.

Also, your function doesn't do what you think it does. I recommend looking into structures; they will be a big help in a case like this. Rather than having three arrays in this odd format, you can have a structure that includes name, scores, and the functions required to interact with the structure.
The problem requires a five string array for names, four double array for individual scores, and five character array for grades. I moved avg = sum/scoreTotal to the bottom of the loop and it is still returning zero. I am thinking of not using the function and finding a way to get the averages to run in main.
Topic archived. No new replies allowed.