I'm writing a program that is supposed to be able to bubble sort data by name and by average. The data I'm given is student last name followed by 3 scores, all put into a text document. I'm to read the names and scores into their respective parallel arrays, and get the averages of the scores for each student put into a 3rd and final array. Looks like this:
Lastname 90 91 92
Lastname 80 81 82
This is the code I have for the read function:
int read(char filename[], char names[][30], int scores[][3], double averages[])
{
int sum = 0; // the base of finding the average
int numStu = 0; // how many students we will have
int col = 0; // column for names
// read text document
ifstream infile(filename); // define
infile.open(filename); // open
for (int i = 0; i < 100; i++)
{
infile.getline(names[i], 30, ' '); // get the name, stop when white space is found
if (! names == 'z') // no names start with lowercase 'z'
{
for (int j = 0; j < 4; j++)
{
infile >> scores[i][j]; // start plugging in scores
sum += scores[i][j]; // while we're at it, get the sum of the scores
}
averages[i] = sum / 3.0; // get the average for this name
}
numStu = i;
}
infile.close(); // close
return numStu;
}
This is the code for my write function (outfile stuff in comments because I'm trying to make this work with screen output first):
void write(char filename[], ios::openmode, char description[], char names[][30], int scores[][3], double averages[], int numberOfStud)
{
// write to text file
// ofstream outfile(filename, mode); // define
cout << description << endl;
for (int i = 0; i < numberOfStud; i++)
{
cout << names[i] << ' '; // print the name
cout << fixed << setw(7) << setprecision(2) << averages[i] << ' ';
// then the averages
for (int j = 0; j < 4; j++)
{
cout << scores[i][j] << ' '; // then the three exam scores
}
cout << endl; // then go to the next line
}
// outfile.close();
}
I'm having a problem with garbage output. It comes up with long lines of irrational numbers, intersparsed with many 0's. I'm not sure what I'm missing here. I think the problem is located in the read function.