//------------------------------------------------------------------- // File :application.C // Author :Alicia Thorsen // Course :CS1129 // // Implementation of the Application class. //------------------------------------------------------------------- #include "application.h" #include "course.h" #include #include #include using namespace std; //------------------------------------------------------------------- // Precondition: filename contains the name of the input file. // Postcondition: The catalog array contains all the information from the file. //------------------------------------------------------------------- void Application::readInputFile(string filename) { int i = 0, numOfStudents; string courseName, studentName; double numericGrade; ifstream fileIn(filename.c_str()); // Check if file opened successfully. if (!fileIn.is_open()) { cerr << "Input file " << filename << " does not exist." << endl; exit(1); } // Read first course name from file if it exists. getline(fileIn, courseName); while (!fileIn.eof()) { // Set name of this course. catalog[i].setName(courseName); // Read number of students in this course. fileIn >> numOfStudents; fileIn.ignore(); // For each student, read his/her name and grade. for (int j = 0; j < numOfStudents; ++j) { getline(fileIn, studentName); fileIn >> numericGrade; fileIn.ignore(); // Add this student to the course. catalog[i].addStudent(studentName, numericGrade); } i++; // Read next course if it exists. getline(fileIn, courseName); } fileIn.close(); // Save number of courses read. numOfCourses = i; } //------------------------------------------------------------------- // Precondition: The catalog array contains all the course information. // Postcondition: Letter grades are computed for all courses. //------------------------------------------------------------------- void Application::calcLetterGrades() { // Calculate letter grades for each course. for (int i = 0; i < numOfCourses; ++i) catalog[i].calcLetterGrades(); } //------------------------------------------------------------------- // Precondition: The catalog array contains all the course information. // Postcondition: All student data for each course is written to an output file. //------------------------------------------------------------------- void Application::writeOutputFile() const { ofstream fileOut(OUTPUT_FILE_NAME); for (int i = 0; i < numOfCourses; ++i) catalog[i].outputCourseData(fileOut); fileOut.close(); cout << "Results written to " << OUTPUT_FILE_NAME << endl; }