I'm trying to read the name and the scores from an input file.
This is my code:
struct Performer
{
string name;
double scores[5];
double final;
};
Performer *readPerfData(string &filename, int &n);
int main( void )
{
string inFileName[] = {"performers.txt", "contestants.txt", "test.txt", "testempty.txt", ""};
int choice = 1; // to stop the program to allow the user to see the results one file at a time
int n;
Performer *performer;
for (string *ptr = inFileName; choice == 1 && *ptr != ""; ptr++) // test loop: takes the names of 4 input files from an array
{
printWelcome();
performer = readPerfData(*ptr, n);
if (performer){
writeFile(performer,n);
printEnd();
cout << "Please enter 1 to continue 0 to stop" << endl;
cin >> choice;
}
}
delete [] performer;
return 0;
} // main
Performer* readPerfData(string &filename, int &n) // <== add parameters, change void if needed
{
cout << "Read Performers Data...\n";
ifstream inFile;
Performer* stu;
inFile.open("performers.txt");
if (inFile.fail())
{
cout << "Unable to open file: "<< filename << endl; // show message if the file is not available
return NULL;
}
if (inFile.is_open())
{
inFile >> n; // read the number of student
stu = new Performer[n]; // allocate new array with n size
for (int i = 0 ; i < n ; i++)
{
inFile.ignore();
getline(inFile, stu[i].name, ';');
for (int j =0; j< 5 ; j++)
{
inFile>> stu[i].scores[j];
}
}
}
return stu;
}
void writeFile(Performer *per, int n) // <== add parameters, change void if needed
{
cout << "Write report to file\n";
for (int i=0 ; i < n ; i++)
{
cout<< "Name: " <<per[i].name;
cout <<". Scores are: ";
for (int j=0; j < 5 ; j++)
{
per[i].scores[j];
}
cout <<endl;
}
}
But my output doesn't show any scores:
Welcome
Read Performers Data...
Calculate Score...
Insertion Sort Descending...
Write report to file
Name: John Lee. Scores are:
Name: David T. Ng. Scores are:
Name: Mary Johnson. Scores are:
Name: Andy V. Garcia. Scores are:
Name: Ann Peterson. Scores are:
Name: Lucy A. Smith. Scores are:
Name: Dan Nguyen. Scores are:
Name: Sue K. Warren. Scores are:
Good Bye!