Calculating average from input file

Hello everyone. For an assignment I'm supposed to calculate and display the averages for test scores and drop the lowest test score in the process. Basically I take a text file with something like this in it:

1
2
3
4
5
6
4
Adams 97 29 100 88
Brown 99 88 77 66
Carter 94 55 66 77
Davis 0 22 77 88
Wilson 77 55 88 55


And I use that to create a program outputting something like this:
1
2
3
4
Student  Average
 Adams       95
 Brown       88
 ...


The top number is used to set how many grades are to be calculated and like I said the lowest is dropped. My program is below and I keep getting this output for the data that I showed you above:

1
2
3
4
5
6
Student  Average
Adams        75
Brown        88
Carter        71
Davis        33
Wilson        73


I've scoured through this and can't for the life me figure out why it's calculating the wrong average. If anyone could help it would really help me out a lot.

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;



int main ()

{
	int totalGrades;      // This is the total number of test grades
	int grade;            // This is the student's test grade
	ifstream inFile;      // This is a declaration of the inFile that holds all the grades
	string fileName;      // This is the filename that the user will enter
	string name;          // This is the name of the student
	int gradeCount;       // This is to keep count of the number of grades entered
	const int MIN = 101;  // This is a constant MIN used to find the lowest score
	int min;              // The lowest score
	int sum;              // The sum of all the grades
	float average;        // The average of all the grades
	
	
	
	// Prompts the user to input the file name that will be read and assigns it
	//  to fileName
	
	cout << "Enter the input file name: ";
	cin >> fileName;
	
	// Open the file with the grades
	
	inFile.open(fileName.c_str ());
	
	// Check to make sure the file opened correctly
	
	if (!inFile)
	{
		cout << "File did not open correctly" << endl;
		return 1;
	}
	
	// Read in the number of tests
	
	inFile >> totalGrades;
	
	// Display the output heading
	
	cout << "Student" << setw(9) << "Average" << endl;
	
	// Read in the first student name
	
	inFile >> name;
	
	// Begin the end of file loop
	
	while (inFile)
	{
		// Print out the name
		cout << name;
		
		// Set gradeCount equal to zero
		
		gradeCount = 0;
		
		// Initialize the lowest grade
		
		min = MIN;
		
		// Initialize the sum
		
		sum = 0;
		
		// Calculate and display the student's average with one score dropped
		
		while (gradeCount < totalGrades)
		{
			// Read in the grade
			
			inFile >> grade;
			
			// Find the sum of the the grades
			
			sum = sum + grade;
			
			// Find out if the grade is the lowest grade
			
			if (grade < MIN)
				min = grade;
			
			gradeCount++;
		}
		
		// Calculate and display average
		
		average = (sum - min) / (totalGrades - 1);
		
		cout << setw(10) << average << endl;
		
		inFile >> name;
			
	}
	
	return 0;
}
1
2
if (grade < MIN)
    min = grade;

Check out this if statement...
Wow thank you so much for your help. I knew it was something really small like that.

I guess the only question I have left is how do I make my average be a floating point number and how do I get the numbers on the output to line up?
how do I make my average be a floating point number

You can cast one of the operands in (sum - min) / (totalGrades - 1) to be a float. Or, you can simply declare sum and/or min to be floats.

how do I get the numbers on the output to line up?

When you cout<< the name, you could use setw on that as well.
Last edited on
When I try to get them to line-up the longer the last name the more off the number is. I just want the numbers to line up and using setw doesn't seem to do anything.
I mean setw on the name. Here, the lengths of the names are less than or equal to 6, so you could do:

cout << setw(6) << name;

With the names lined up, then the averages should line up as well.
Thanks for all your help tonight. You've got it fixed for me and I appreciate that.
Topic archived. No new replies allowed.