// course.cpp // ---------------------------------------------------------------------- // functions for reading Course data from a file, printing Course // arrays, finding time conflicts // // CSE 143 // Homework 1 // http://www.cs.washington.edu/education/courses/143/00su/homework/hw1/ // 20 Jun 2000 // // your name and e-mail address // your student number // your section ID #include #include #include #include #include "course.h" ////////////////////////////////////////////////////////////////////// // preconditions: fileName contains a valid string // // postcondition: if fileName was a valid course file, the data has // been placed in the array, and countCourses is set to the number of // courses read; if fileName was not valid, countCourses is set to -1 // (Do not modify this function.) void readCourseFile(char fileName[], Course courseList[], int maxCourseCount, int &countCourses) { assert(maxCourseCount > 0); // Mac and UNIX users: omit ios::nocreate argument ifstream inFile(fileName, ios::nocreate); if (! inFile.good()) { cerr << endl << "unable to open file: " << fileName << endl; countCourses = -1; return; } // counts number of courses read so far countCourses = 0; while (inFile.good() && (countCourses < maxCourseCount)) { // temporary variable while we fill in the data so that we only // have to deal with array indexing once at the end Course c; // read name, then lecture start time and duration, both in units // of hours inFile.getline(c.name, MAX_NAME_LENGTH); inFile >> c.meetingTime >> c.duration; // very simple error-checking // meeting times must be on the hour in 24-hour format if (c.meetingTime < 0 || c.meetingTime > 23) { cerr << "course " << c.name << ": meeting time out of range 0-23" << endl; countCourses = -1; return; } // duration should be in hours if (c.duration <= 0) { cerr << "course " << c.name << ": meeting duration must be positive, nonzero" << endl; countCourses = -1; return; } // we need to skip the trailing whitespace after times; // eat trailing characters, including \n char line[MAX_NAME_LENGTH]; inFile.getline(line, MAX_NAME_LENGTH); // copy the struct, filled with data courseList[countCourses] = c; countCourses++; } } // end readCourseFile ////////////////////////////////////////////////////////////////////// // *** define functions printCourseList, reportTimeConflicts here ***