This probably has a really simple fix but I cant for the life of me see it. I have two input files one with courses and one with students. For students it parses data and adds the object into a list. I’m trying to get it so that when I go to output the student objects with similar data from the list (ex: Doe,John,1233485,ART Doe,John,1233485,MATH) it doesn’t output each object, but one object with cumulative data. (I don’t know if I’m explaining it the best sorry-)
`// Parse student data
while (getline(fileStudent, studentLine)) {
istringstream iss(studentLine);
string lastName, firstName, SID, courseList;
// Parse the CSV line
if (getline(iss, lastName, ',') && getline(iss, firstName, ',') &&
getline(iss, SID, ',') && getline(iss, courseList)) {
// Calculate total credit hours for the student
int totalCreditHours = 0;
// Iterate over the courses the student has requested
istringstream courseStream(courseList);
string courseName;
while (getline(courseStream, courseName, ',')) {
// Look up the course
auto it = courses.find(Course(courseName, 0, 0, 0));
if (it != courses.end()) {
// Increment enrollment count for the course
it->incrementEnrolled();
// Add the student to the course roster
Course& course = const_cast<Course&>(*it); // Remove const
course.enrollStudent(SID);
// Update total credit hours
totalCreditHours += it->getCreditHours();
}
}
// Create the student object and add it to the list
Student student(lastName, firstName, SID, courseList, totalCreditHours);
students.push_back(student);
}
}
fileStudent.close();`
I’ve tried altering my code several times to combat this. To no avail my program still outputs all of the similar objects not adding anything together.
Puppy Puppy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.