I can assign the FirstName, LastName & StudentId of a text file to elements in a structure but what I am stuck on is how to loop the program NumberOfGrades times. I know that I need a "for" loop but what is not clicking for me is how to get the "getlin" function to pick up the values from there. I have the for loop commented out because it wasn't working.
//-----------------------------main body
int main()
{
cout << "Welcome to the GradeCaculator Deluxe." << endl;
GetStudentList ();
GetClassHours ();
// getline(File1) ;
//return 0;
}//end main
//--------GetStudentList
string GetStudentList()
{
string FirstName;
string LastName;
int StudentId;
int NumberOfGrades;
int Index = 0;
int LineIndex = 0;
//Students MyStudents;
ifstream inputFile;
string File1;
//int NumberOfLines = 0;
cout <<"Enter the file name for the list of Students, including the extension::" << endl;
getline(cin, File1);
inputFile.open(File1.c_str());
if(!inputFile.is_open())
{
exit(EXIT_FAILURE);
}
// while (getline (inputFile, File1))
//{
//++LineIndex;
//}
//cout << "Number of Lines in text file is: " << LineIndex << endl;
while (inputFile >> FirstName >> LastName >> StudentId >> NumberOfGrades)
{
MyStudents.FirstName = FirstName;
MyStudents.LastName = LastName;
MyStudents.StudentId = StudentId;
//for (int Index = 0; Index >= NumberOfGrades; ++Index)
//{
// (inputFile >> ClassNumber >> Grade)
//}
}
cout << MyStudents.FirstName << ", " << MyStudents.LastName << ", " << MyStudents.StudentId << endl;
cout << "\nThe file has been successfully opened for reading.\n";
return File1;
}//end GetStudentList
//--------GetClassHours
string GetClassHours()
{
Grades MyHours [15];
ifstream inputFile;
string File2;
cout <<"Enter the file name for the list of Class Hours, including the extension::" << endl;
getline(cin, File2);
inputFile.open(File2.c_str());
if(!inputFile.is_open())
{
exit(EXIT_FAILURE);
}
cout << "\nThe file has been successfully opened for reading.\n";
return File2;
}//end GetClassHours
-----Contents of Students.txt----
Harry Houdini 1212 3 MAG101 A PRES201 A ESC301 A
Henry Ford 2211 2 MFG110 A SAL202 B
Albert Einstein 1155 1 CALC100 F
Alan Turing 9082 4 CSC110 A CSC201 A CSC210 A CSC205 A
Melvil Dewey 1876 2 LIBM102 C SUPV202 D
struct student
{
string FirstName;
string LastName;
int StudentId;
int NumberOfGrades;
}MyStudents;
by
struct student
{
string FirstName;
string LastName;
int StudentId;
int NumberOfGrades;
}MyStudents[10];
or
struct student
{
string FirstName;
string LastName;
int StudentId;
int NumberOfGrades;
}MyStudents*;
I am still having issues assigning parts of the text file to the structure. I am getting the following error:
[Error] no match for operator []' in MyStudents[index1]'
[Error] no match for operator []' in MyStudents[index2]'