Need Help, cant get the average to work right. Thanks

my code pulls numbers form a txt file and displays them, i have my average number in the right spot but its not averaging, its just displaying the number 5.

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
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{

	ifstream gradeFile;	
 	gradeFile.open("ScoreData.txt");

    if(gradeFile.fail())
    {	
	cout << "File not found. Program terminated." << endl;
	return 1;
    }
	
	char initial1;		// initial one
	char initial2;		// initial two
	int score;		// student's test scores
	int numscores = 1;
	int sum = 0;
	int average;

	while(!gradeFile.eof())
	{
		gradeFile >> initial1 >> initial2;
		cout << "\n" << initial1 << "." << initial2 << ".";
		
		do
		{
			gradeFile >> score;
			if(score != -1)
				cout << setw(4) << score;
			sum += score;
			numscores++;
		
		}
		while(score != -1);
		
		average = sum / numscores;
		cout <<setw(3) << " " << average; 
	}
	
	gradeFile.close();
	cout << "\nAll Student Processed." << endl;

	return 0;
}



A.A.    5  11   9   5    8   5   3   5
R.D.   11   2   3  12    8  11   9   5
J.E.   10   5
K.H.    4  11   5   4    6   8   2   6   11    5   5
G.M.   10   1   1   9    3   5
S.O.    3   1   6   5
All Student Processed.
Press and key to continue...


The last 5 in each row is the average number.

Thank You for any help.
Ok my bad it does have the right average number, because it using the -1 at the end of each row that's not displayed. its using it in the sum += score count and the numscores count. xD I was looking over my own code after I posted it and say this.

On to a different question how do i make it so the display looks like this.

A.A.    5  11   9   5    8   5   3                 5
R.D.   11   2   3  12    8  11   9                 5
J.E.   10                                          5
K.H.    4  11   5   4    6   8   2   6   11    5   5
G.M.   10   1   1   9    3                         5
S.O.    3   1   6                                  5
All Student Processed.
Press and key to continue...
When it comes to displaying, that is all graphical stuff. You have to play with cout for the console or your windows handles for the windows programs. You can auto add spaces dependeing on the size of numbers (So if if you alot 4 spaces for numbers, then for anything over 999 you need 1 space, anything less then 10 you need 4 spaces).

Topic archived. No new replies allowed.