Need some explanation.

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?
Change the return type to what you really want to return:

Performer *readPerfData(string filename, int &n) // Note: You need a prototype before main() in order to use it

You use it like so:
1
2
3
4
5
6
7
8
9
for (string *ptr = inFileName; choice == 1 && *ptr != ""; ptr++) // test loop: takes the names of 4 input files from an array
{
Performer *stu = readPerfData(*ptr, n);
for (int i = 0; i < n; i++)
{
cout << stu[i].name << stu[i].scores;
}
delete[] stu;
}
Topic archived. No new replies allowed.