0

test file

11111,Smith,Bob,10,9,8,10,9,10,92,89,90
11112,Doe,Jean,8,9,8,7,9,9,84,88,89
11113,Hardy,Joe,9,9,10,9,8,9,88,90,95

information stored in

struct Student {
    std::string ID;
    std::string lName;
    std::string fName;
    int quiz1;
    int quiz2;
    int quiz3;
    int quiz4;
    int quiz5;
    int quiz6;
    int test1;
    int test2;
    int final;
    float total;
};

problem: when adding grades it will only read the first grade correctly. I've tried a number of different methods but this one is the only one so far that even partially works :( test values do work properly when a student struct is manually set; so the file read in is the issue

int main() {

    //add a place to store student info
    StudentGrades session;
    string inFile = "classGrades.txt";

    //open input file if possible or display an error message and exit with failure
    ifstream in;
    cout << "Opening File: " << inFile << "\n";
    in.open(inFile);
    if (in.is_open()) {
        cout << "File Open: " << inFile << "\n";
        string line;
        while (getline(in,line)) {
            //read in student info from file line by line to a Student object for each line
            Student student;
            std::stringstream stream(line); //record info from file
            getline(stream,student.ID,',');
            getline(stream,student.lName,',');
            getline(stream,student.fName,',');
            stream >> student.quiz1;
            stream >> student.quiz2;
            stream >> student.quiz3;
            stream >> student.quiz4;
            stream >> student.quiz5;
            stream >> student.quiz6;
            stream >> student.test1;
            stream >> student.test2;
            stream >> student.final;

            session.add(student);
       }
    }
    else
    {
        cout << "File could not be opened: " << inFile << "\n";
        exit(1);
    }
    //close the input file
    cout << "Closing File: " << inFile << "\n";
    in.close();
    cout << "File Closed: " << inFile << "\n";

    session.printStudentList(cout);

    return 0;
}

output from file

Opening File: classGrades.txt
File Open: classGrades.txt
Closing File: classGrades.txt
File Closed: classGrades.txt
ID    | Last Name  | First Name  |    Q1 |    Q2 |    Q3 |    Q4 |    Q5 |    Q6 |    T1 |    T2 | Final | Total
11113 | Hardy      | Joe         |     9 |     0 |     0 |     0 |     0 |     0 |     0 |     0 |     0 |  2.25
11112 | Doe        | Jean        |     8 |     0 |     0 |     0 |     0 |     0 |     0 |     0 |     0 |     2
11111 | Smith      | Bob         |    10 |     0 |     0 |     0 |     0 |     0 |     0 |     0 |     0 |   2.5
1
  • 2
    E.g. stream >> student.quiz2; will not handle the comma. Feb 10, 2021 at 17:41

1 Answer 1

3

Your problem here is that unlike getline, which throws out the delimiter, stream >> student.quiz1; doesn't so there is a , in the stream when you try to do stream >> student.quiz2;. You need to get that comma out of the stream. You can do that by using get to "eat" the character, or use a named char like

char eat_comma;

//...

stream >> student.quiz1 >> eat_comma;
stream >> student.quiz2 >> eat_comma;
stream >> student.quiz3 >> eat_comma;
stream >> student.quiz4 >> eat_comma;
stream >> student.quiz5 >> eat_comma;
stream >> student.quiz6 >> eat_comma;
stream >> student.test1 >> eat_comma;
stream >> student.test2 >> eat_comma;
stream >> student.final; // no comma after the last value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.