My code is like this:
struct Performer
{
string name;
double scores[5];
double final;
};
int main( void )
{
int n;
Performer stu;
string inFileName[] = {"performers.txt", "contestants.txt", "test.txt", "testempty.txt", ""};
for (string *ptr = inFileName; choice == 1 && *ptr != ""; ptr++) // test loop: takes the names of 4 input files from an array
{
readPerfData(*ptr, n);
}
int readPerfData(string filename, int &n)
{
cout << "Read Performers Data\n";
ifstream inFile;
inFile.open(filename.c_str());
if (inFile.fail())
{
cout << "Unable to open file: "<< filename << endl; // show message if the file is not available
return NULL;
}
inFile >> n; // read the number of student
Performer *stu = new Performer[n]; // allocate new array with n size
for (int i = 0; i < n; i++)
{
inFile >> stu[i].name >> stu[i].scores;
}
inFile.close();
return stu;
}
How can i reads names and scores from an input file into a dynamically allocated array of structures?