Hi I've been trying to read elements of a file to my class variables but it's giving me a read error. Would be great if someone could look through my code, I mainly need help with adding all 4 students into student class objects. Perhaps an array of student objects.
My file that I am reading from is ProgramSixData.txt:
George
75,85,95,100,44
Peter
100,100,100,100,100
Frank
44,55,66,77,88
Alfred
99,88,77,66,55
void main()
{
fstream File;
string FileName = "ProgramSixData.txt";
bool FoundFile;
string Line;
Student testStudent;
do {
File.open(FileName, ios_base::in | ios_base::out);
FoundFile = File.is_open();
if (!FoundFile)
{
cout << "Could not open file named " << FileName << endl;
File.open(FileName, ios_base::out); // try to create it
FoundFile = File.is_open();
if (!FoundFile)
{
cout << "Could not create file named " << FileName << endl;
exit(0);
}
else;
}
else;
} while (!FoundFile);
do {
File >> testStudent;
if (File.fail())
{
cout << "Read Failed" << endl;
cout << "Bye" << endl;
exit(0);
}
else;
testStudent.display();
} while (!File.eof());
cout << "Bye" << endl;
File.close();
}
How do I read each of the names and associated grades from the text file into the class variables? How do I make the file stop reading after it's done with one student's name and grades and create the next class object student2 for the second student's names and grades?