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.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
usingnamespace 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
constint 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;
}
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.