0
string courseName, catName;
double weight, score, outOf;

ifstream ifs(filename);
if (!ifs.is_open()) {
    throw std::invalid_argument("Cannot open file");
}

// CHECK IF FILE IS EMPTY
if (ifs.peek() == '\n') { return; }

string line;
while (ifs.good()) {
    getline(ifs, courseName);
    Course c(courseName);
    getline(ifs, line);
    istringstream ss(line);
    while (ss.good()) {
        getline(ss, catName, ';');
        ss >> weight;
        WeightCategory w(catName, weight);  // Make a new category with given name and weight
        string grades;
        getline(ss, grades, ';');
        istringstream cs(grades);
        while (cs >> score >> outOf) {
            Grades g(score, outOf);
            w.pushGrade(g);
        }
        c.pushCategory(w);
        cout << "Pushing back category: " << w.getName() << endl;
    }
    courses.push_back(c);
    line.clear();
}
ifs.close();

Hello! I am trying to make a program that reads from a file. I wrote my data structure down below. It holds the name of the course, the category (lab, homework, exam), the weight of that category on the total score, and each individual assignment scores followed by whatever the score is out of (5/10, 100/100, 20/40).

DATA STRUCTURE

course name

category name;weight score outOf;

with potential to repeat like:

course name 2

category name; weight score outOf; category name; weight score outOf score outOf;

course name 3

category name; weight score outOf score outOf score outOf score outOf;

course name 4

category name; weight; category name; weight score outOf; category name; weight score outOf score outOf

The Problem:

When I try to read from this file: grades data

It reads beyond the last category and output this: output

So far, I'm content with how it handles empty categories and that name appearing as "64.2c" is not my issue. That wasn't even happening before and I'm not sure why it's happening now. But regardless, I would like to know why my program reads that last empty "category" after "Labs". I'm also open to dialogue and discussion about how good or bad my code is for a Grade Calculator program that stores data for later viewing.

Thank you!

F. Doe
  • 9
  • 1
  • I believe `while (ss.good()) {` is your bug. Related: [https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm Nov 27 '20 at 15:37

0 Answers0