Hello alfie nsugh,
My bad, I was thinking to far ahead of where I was working again. "start" was defined and initialized to a value of 3. Because in the vector the name is at element zero, midterm at element 1 and final at element 2.
May be by accident name 1, name 2 and name 3 just happened to be spaced 13 apart which happens to work fine for what you have right now. Should this spacing ever change your code would not likely work.
While looking in the "Student_info.cpp" file I noticed two functions that are never used, so I used them. I did have to change the functions around, but kept the concept.
To start with I changed the input file to:
firstStudent 7 9 5 4 8 5 9 7 8 6 5 4
secondStudent 7 8 5 7 8 6 77 89 6 78 68 77
John_Doe 8 9 50 77 66 85 80 75 91
thirdStudent 8 9 8 8 7 8 89 7 7 7 7
Jane_Doe 100 100 100 100 100 100 100 100 100 100 100 100
|
And yes that is a blank line at the end of the file.
In main all I had to do is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
int main()
{
// <--- Removed unused variables.
list<Student_info> students;
Student_info record;
string line;
ifstream inFile;
//inFile.open("D:\\projects\\grades\\text.txt");
//inFile.open("text.txt");
inFile.open("text 2.txt"); // <--- One line per name.
// <--- This should work better for you.
if (!inFile)
{
cout << "Unable to open file text.txt";
std::this_thread::sleep_for(std::chrono::seconds(5)); // Requires header files "chrono" and "thread"
exit(1); // <--- no need to continue.
}
while (getline(inFile, line))
{
vector<string> v(split(line));
read(record, v);
students.push_back(record);
record.homework.clear();
//std::cout << std::endl; // <--- Used for debugging as a break point.
}
inFile.close();
// <--- Rest of code for printing to the screen.
|
In the "Student_info.cpp" file I had to change two functions:
The "read" function:
1 2 3 4 5 6 7 8 9
|
void read(Student_info& record, std::vector<std::string>& v)
{
// read and store the students name and midterm and final exam grades
record.name = v[0];
record.midterm = std::stod(v[1]);
record.final = std::stod(v[2]);
read_hw(record, v);
}
|
And the "read_hw" function:
1 2 3 4 5 6 7 8 9 10
|
void read_hw(Student_info& record, vector<std::string>& hw)
{
//double x;
for (size_t lc = 3; lc < hw.size(); lc++)
{
//read homework grades
record.homework.push_back(std::stod(hw[lc]));
}
}
|
Just an idea.
Hope that helps,
Andy