I couldn't read the file from a text completely. Can someone help me out?
My code is like this:
Performer* readPerfData(string filename, int &n)
{
cout << "Read Performers Data...\n";
ifstream inFile;
inFile.open("performers.txt");
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++)
{
getline(inFile,stu[i].name);
inFile.ignore(0,';');
for (int j =0; j<5; j++)
{
inFile >> stu[i].scores[j];
}
}
inFile.close();
return stu;
}
the performers.txt is like this:
performers.txt
8
John Lee; 7.0 7.8 7.1 7.9 7.5
David T. Ng; 8.3 2.9 9.8 9.2 9.7
Mary Johnson; 8.3 8.0 8.9 7.9 8.5
Andy V. Garcia; 9.1 8.9 9.0 8.7 9.1
Ann Peterson; 9.0 8.8 9.1 9.7 9.3
Lucy A. Smith; 8.2 8.4 8.9 9.3 8.5
Dan Nguyen; 9.0 5.6 8.9 9.9 7.3
Sue K. Warren; 7.9 8.2 7.6 8.1 8.0
And the output is:
Sue K. Warren; 7.9 8.2 7.6 8.1 8.0
Ah you're right, it's currently double-spacing the output but luckily doesn't affect the numbers. Thanks for the return tip. Btw welcome back after the hiatus.
This is what I got so far:
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;
if it did not read the scores its probably out of sync with the file.
don't try to read integers right now.
take that out, and read just a throw-away string. then print what you got to the console with cout and endl. see what the program is getting in your loops.
compare that to what is in the file. see where it is getting lost and try to fix it this way, then you can put the integers back once it is doing what is expected.