So here is the problem. I have a file that starts with a number, this number says how many entries are in the file (5 means there are 5 names). Each entry takes up two lines in the txt file. First line is the name, the second is a group of four "grades". I get all of this to work great, but I don't understand this: When entering the filename hwk9.txt into the program, I get an empty first spot in the names array. Any other name I have tried works perfectly. I have tried compiling in Bloodshed and the course's online complier as well, no dice.
So the question I am wondering about is this, given multiple files with the same formatting, what would cause the one file (that happens to be the same name as the test file for the assignment that cannot be altered) to give a different result? How does the file "hwk9.txt" give a blank value to namesArray[0] but none of the others do? See lines 38-72 for the code relevant to this.
/*****************************************
* Computer Science 221
* Kevin Burdette
* Date: 11/28/2009
* Instructor: Dr. Otha Britton
* This program analyzes input data to return averages
*****************************************/
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
void loadFileToArray (string namesArray[], double gradesArray[][4], int &numberOfStudents);
void displayNamesAndGrades (string namesArray[], double gradesArray[][4], int numberOfStudents);
void calculateAveExamGradeAndDisplay(double grades[][4], int numberOfStudents);
int main ( )
{
string names[30];
double grades[30][4];
int numberOfStudents;
loadFileToArray (names, grades, numberOfStudents);
displayNamesAndGrades (names, grades, numberOfStudents);
calculateAveExamGradeAndDisplay (grades, numberOfStudents);
system("pause");
return 0;
}//end of main
void loadFileToArray (string namesArray[], double gradesArray[][4], int &numberOfStudents)
{
string filename;
ifstream input;
cout << "What is the name of the file to open and analyze?" << endl;
cin >> filename;
input.open(filename.c_str());
if (input.fail())
{
cout << filename << " was not found." << endl;
cout << "Terminating the program . . . ." << endl; //Closes the function if the file does not open properly.
system("pause");
exit(99);
}//end of fail close statement
input >> numberOfStudents;
while(!input.eof())//continue until end of file
{
char junk;
for (int row = 0; row < numberOfStudents ; row++ )
{
input.get(junk); //Gets rid of the new line character
getline( input, namesArray[row]);
for( int col = 0; col < 4 ; col++)
{
input >> gradesArray[row][col];
}//end of inner for loop
}//end of input for loop
}//end of while
input.close();
return;
}//end of loadFileToArray
void displayNamesAndGrades (string namesArray[], double gradesArray[][4], int numberOfStudents)
{
char letterGrade;
double average;
double sum;
cout << setw(30) << left << "Names" << right << setw(10) << "Ave. Score"
<< setw(10) << "Grade" << endl;
cout << "--------------------------------------------------" << endl;
for (int row = 0; row < numberOfStudents; row++)
{
sum = 0;
for (int col = 0; col < 4; col++)
{
sum += gradesArray[row][col];
}
average = (sum / 4);
if (average >= 90)
letterGrade = 'A';
else if (average >= 80 && average < 90)
letterGrade = 'B';
else if (average >= 70 && average < 80)
letterGrade = 'C';
else if (average >= 60 && average < 70)
letterGrade = 'D';
else
letterGrade = 'F';
cout << setprecision(2) << showpoint << fixed;
cout << setw(30) << left << namesArray[row] << setw(10) << right << average
<< setw(10) << letterGrade << endl;
}
return;
}//end of grades
void calculateAveExamGradeAndDisplay(double grades[][4], int numberOfStudents)
{
double sumC0, sumC1, sumC2, sumC3;
sumC0 = 0;
sumC1 = 0;
sumC2 = 0;
sumC3 = 0;
for (int row=0; row < numberOfStudents; row++)
{
sumC0 += grades[row][0];
sumC1 += grades[row][1];
sumC2 += grades[row][2];
sumC3 += grades[row][3];
}
cout << endl;
cout << "The average of exam 1 is " << sumC0 / numberOfStudents << endl;
cout << "The average of exam 2 is " << sumC1 / numberOfStudents << endl;
cout << "The average of exam 3 is " << sumC2 / numberOfStudents << endl;
cout << "The average of exam 4 is " << sumC3 / numberOfStudents << endl;
}