bool GPA::importGrades(string fileName) { if (!checkGradeFile(fileName)) { //cout << "One file bad: " << fileName << endl; return false; } ifstream gradesIn; gradesIn.open(fileName.c_str()); string id; while(getline(gradesIn, id)) { unsigned long long int idin; stringstream ss; ss << id; ss >> idin; string className; string letterGrade; getline(gradesIn, className); getline(gradesIn, letterGrade); double GPA = findGPA(letterGrade); if (mapFindStudent(idin) != NULL) { StudentInterface* mapStudent = new Student(); delete mapStudent; mapStudent = mapFindStudent(idin); mapStudent->addGPA(GPA); } else if (setFindStudent(idin) != NULL) { //cout << "HERE: " << GPA << endl; StudentInterface* setStudent = new Student(); delete setStudent; setStudent = setFindStudent(idin); setStudent->addGPA(GPA); } } gradesIn.close(); return true; }
/* * importGrades() * * Read in and parse through the given file. Each part of an entry in the file * is given on a separate line in the file. Student ID is first, course is * second, and grade is last. There are no blank lines between entries. The * following is an example file: * * 5291738860 * CHEM134 * A * 9251734870 * BOT180 * B * 9251733870 * PE273 * D+ * 5291738760 * HIS431 * A- * * Compute the GPA by finding the average of all the grades with a matching student ID * in the Grade file. The GPA is calculated by taking a Student's total sum GPA and * dividing by the number of classes taken. If the given student ID has no matching * grades in the Grade file, the GPA is 0.00. It is not necessary to store the course * names so long as the total number of courses taken is counted. * * You may assume that the given student ID exists in the map or set. * * Use the following point values for each grade. * * A = 4.0 A- = 3.7 * B+ = 3.4 B = 3.0 B- = 2.7 * C+ = 2.4 C = 2.0 C- = 1.7 * D+ = 1.4 D = 1.0 D- = 0.7 * E = 0.0 * * Returns false if an invalid filename is given, otherwise true. */ bool GPA::importGrades(string fileName) { string student_id; string course_name; string grade; StudentInterface* student = NULL; ifstream file; file.open(fileName); if (!isValidClass(fileName)) { file.close(); return false; } typedef set<StudentInterface*, Comparator>::iterator it; while (getline(file, student_id)) { unsigned long long int ID = atoi(student_id.c_str()); getline(file, course_name); getline(file, grade); // grabbed each line, now need to check if the student_id is equal to any in the set and map // if it is, add that to the gpa and add to num classes // either in set or map if (myMap[ID] == NULL) // not in map, it is in set { myMap.erase(ID); it itr; for (itr = mySet.begin(); itr != mySet.end(); itr++) { if ((*itr)->getID() == ID) { student = *itr; } } } else { student = myMap[ID]; } if (grade == "A") { student->addGPA(4.0); } else if (grade == "A-") { student->addGPA(3.7); } else if (grade == "B+") { student->addGPA(3.4); } else if (grade == "B") { student->addGPA(3.0); } else if (grade == "B-") { student->addGPA(2.7); } else if (grade == "C+") { student->addGPA(2.4); } else if (grade == "C") { student->addGPA(2.0); } else if (grade == "C-") { student->addGPA(1.7); } else if (grade == "D+") { student->addGPA(1.4); } else if (grade == "D") { student->addGPA(1.0); } else if (grade == "D-") { student->addGPA(0.7); } else if (grade == "E") { student->addGPA(0.0); } } file.close(); return true; }