// Function version of // Project 2 for CS 1044 Summer I 2001 // // Programmer: William D McQuain // OS: Windows NT Workstation 4.0 // System: Pentium II 400, 128 MB Memory // Compiler: Visual C++ 6.0, Service Pack 4 // Last modified: June 12, 2001 // // Purpose // This program reads a list of trip data records, including // origin, destination, distance and time. For each trip the // program computes the time in minutes and average MPH. The // overall time for all trips and overall average MPH are also // computed. // // The program then writes a summary of its findings to an output // file. // #include // for cout #include // for file streams #include // for formatting manipulators #include // for string variables #include // for INT_MAX using namespace std; // to put all of the above things in scope const int MINPERHOUR = 60; // Number of minutes in an hour // Function parameters: // void printIdentification(ofstream& Out); void printTableHeader(ofstream& Out); void readTrip(ifstream& In, string& Origin, string& Destination, int& tripMiles, int& tripHours, int& tripMinutes); int convertHHMMtoMin(int tripHours, int tripMinutes); double calcMPH(int Miles, int Minutes); void printTripData(ofstream& Out, string Origin, string Destination, int tripMiles, int tripTime, double tripMPH); void printTableEnd(ofstream& Out); void printSummary(ofstream& Out, int numTrips, int totalMiles, int totalHours, int totalMinutes, double overallMPH); int main() { const string dataFileName = "TripData.txt"; // Names of input file... const string resultsFileName = "Summary.txt"; // ...and output file. ifstream In(dataFileName.c_str()); // Attach an input stream to the input file. // If the input file does not exist, this will detect that. // We handle that by printing an error message and stopping the program. if ( In.fail() ) { cout << "Data file not found: " // Write an error message... << dataFileName // including the file name. << endl // Bang "return" << "Exiting now..." << endl; // Finish the message. return 1; // Terminate a failed execution. } ofstream Out(resultsFileName.c_str()); // Attach an output stream to the output file. Out << fixed << showpoint; // Prepare output stream for decimal output. // Write the header data to the output file: printIdentification(Out); printTableHeader(Out); string Origin, // Name of starting point for trip. Destination; // Name of destination for trip. int tripMiles, // Length of trip in miles. // Time trip took is given as hh:mm so: tripHours, // hours field tripMinutes, // minutes field tripTime; // Time for trip in minutes. int numTrips = 0; // Number of trips reported. int totalMiles = 0, // Total length of all trips, in miles. totalMinutes = 0; // Total time for all trips, in minutes. double tripMPH; // Speed of trip, in miles per hour. In.ignore(INT_MAX, '\n'); // Skip over the two header lines in the In.ignore(INT_MAX, '\n'); // input file. // Try to read the data for the first trip: readTrip(In, Origin, Destination, tripMiles, tripHours, tripMinutes); while ( In ) { // As long as you got new data, continue... numTrips++; // Count this trip. // Calculate the total minutes for this trip: tripTime = convertHHMMtoMin(tripHours, tripMinutes); // Calculate the average MPH for this trip: tripMPH = calcMPH(tripMiles, tripTime); // Update the totals over all trips: totalMiles = totalMiles + tripMiles; totalMinutes = totalMinutes + tripTime; // Print the results for this trip: printTripData(Out, Origin, Destination, tripMiles, tripTime, tripMPH); // Try to read the data for another trip: readTrip(In, Origin, Destination, tripMiles, tripHours, tripMinutes); } // Write a delimiter for the bottom of the output table: printTableEnd(Out); // Check to see if there were any trips; otherwise we'll wind up // dividing by zero later on. if ( numTrips == 0 ) { Out << "No trips were reported." << endl; return 0; // No trips, so stop here. } // Calculate the overall average MPH for all trips: double overallMPH = calcMPH(totalMiles, totalMinutes); // Calculate total hours and residual minutes for all trips: int totalHours; totalHours = totalMinutes / MINPERHOUR; totalMinutes = totalMinutes % MINPERHOUR; // Print the summary information for all trips: printSummary(Out, numTrips, totalMiles, totalHours, totalMinutes, overallMPH); // Close the input and output files: In.close(); Out.close(); // Terminate a successful execution: return 0; } //////////////////////////////////////////////////////////////// printIdentification // Writes the programmer name and course information to the output file. // // Parameters: // Out output stream to which information is written // // Pre: The output stream has been opened. // Nothing has been written yet. // // Post: The specified information has been written to the output stream. // // Returns: None // // Called by: main() // Calls: None // void printIdentification(ofstream& Out) { // Print identification information to output file: Out << "Programmer: " << "Bill McQuain" << endl; Out << "CS 1044 Summer I 2001" << endl; Out << endl; return; } //////////////////////////////////////////////////////////////// printTableHeader // Writes the column headers and top delimiter to the output file. // // Parameters: // Out output stream to which information is written // // Pre: The output stream has been opened. // The programmer identification data has been written. // // Post: The specified information has been written to the output stream. // // Returns: None // // Called by: main() // Calls: None // void printTableHeader(ofstream& Out) { // Print output column headers to output file: Out << "Origin Destination Mileage Minutes MPH" << endl; Out << "-----------------------------------------------------------------" << endl; return; } //////////////////////////////////////////////////////////////// readTrip // Reads the data for a single trip from the input file. // // Parameters: // In input stream // Origin name of place where trip began // Destination name of place where trip ended // tripMiles length of trip in miles // tripHours hours field for elapsed time trip took // tripMinutes minutes field for elapsed time trip took // // // Pre: Input stream has been opened. // Input point is at beginning of a line of trip data. // Trip data line is formatted according to specification. // // Post: Trip data line has been read into the corresponding // parameters. // Input point is at beginning of next input line. // // Returns: None // // Called by: main() // Calls: None // void readTrip(ifstream& In, string& Origin, string& Destination, int& tripMiles, int& tripHours, int& tripMinutes) { getline(In, Origin, '\t'); // Read: the name of the trip origin. getline(In, Destination, '\t'); // the name of the trip destination. In >> tripMiles; // length of trip in miles In >> tripHours; // hours field for trip time In.ignore(1, ':'); // colon separating hours and minutes In >> tripMinutes; // minutes field for trip time In.ignore(INT_MAX, '\n'); // Skip to beginning of next input line. return; } //////////////////////////////////////////////////////////////// convertHHMMtoMin // Converts a time value in HH:MM format to equivalent total minutes. // // Parameters: // tripHours HH field of time // tripMinutes MM field of time // // Pre: HH and MM have been initialized. // HH and MM are non-negative. // MM is in the range [0, 59) // The constant MINPERHOUR is in global scope. // // Post: The equivalent time in minutes has been computed. // // Returns: Equivalent time in minutes: MINPERHOUR * HH + MM // // Called by: main() // Calls: None // int convertHHMMtoMin(int tripHours, int tripMinutes) { return (MINPERHOUR * tripHours + tripMinutes); } //////////////////////////////////////////////////////////////// calcMPH // Calculates the average speed of a trip, in MPH, given the length of // of the trip in miles and the elapsed time required for the trip, in // minutes. // // Parameters: // Miles length of the trip in miles // Minutes elapsed time for the trip, in minutes // // Pre: Miles and Minutes have been initialized. // Miles >= 0 and Minutes > 0. // The constant MINPERHOUR is in global scope. // // Post: The average speed has been computed.. // // Returns: Avg speed in MPH: Miles / (Minutes / MINPERHOUR) // // Called by: main() // Calls: None // double calcMPH(int Miles, int Minutes) { return (MINPERHOUR * double(Miles) / double(Minutes)); } //////////////////////////////////////////////////////////////// printTripData // Writes the summary data for a single trip to the output file. // // Parameters: // Out output stream // Origin name of place where trip began // Destination name of place where trip ended // tripMiles length of trip in miles // tripTime elapsed time for the trip, in minutes // tripMPH average speed for the trip, in MPH // // Pre: Output stream has been opened. // Identification and table headers have been written. // Output stream is at the beginning of a line. // The trip data parameters have been initialized. // // Post: The specified data has been written to the output file. // The output stream is at the beginning of the next line. // // Returns: None // // Called by: main() // Calls: None // void printTripData(ofstream& Out, string Origin, string Destination, int tripMiles, int tripTime, double tripMPH) { Out << Origin; Out << setw(20 - Origin.length()) << ' '; Out << Destination; Out << setw(27 - Destination.length()) << tripMiles << setw(10) << tripTime << setw(8) << setprecision(1) << tripMPH << endl; return; } //////////////////////////////////////////////////////////////// printTableEnd // Writes the bottom delimiter and a blank line to the output file. // // Parameters: // Out output stream to which information is written // // Pre: The output stream has been opened. // The table of trip data has been written. // // Post: The specified information has been written to the output stream. // // Returns: None // // Called by: main() // Calls: None // void printTableEnd(ofstream& Out) { Out << "-----------------------------------------------------------------" << endl; Out << endl; } //////////////////////////////////////////////////////////////// printSummary // Writes a summary of statistics for all the trips. // // Parameters: // Out output stream // numTrips number of trips for which data was read // totalMiles total mileage for all the trips // totalHours HH field of total time for all the trips // totalMinutes MM field of total time for all the trips // overallMPH average speed over all the trips // // Pre: The output stream has been opened. // The identification, table headers, table data and table // bottom have been written. // The numeric parameters have been initialized. // The numeric parameters are non-negative. // MM is in the range [0, 59) // // Post: The specified data and labels have been written. // // Returns: None // // Called by: main() // Calls: None // void printSummary(ofstream& Out, int numTrips, int totalMiles, int totalHours, int totalMinutes, double overallMPH) { Out << "Number of trips: " << setw(8) << numTrips << endl; Out << "Total miles: " << setw(8) << totalMiles << endl; // Print the total time and overall MPH for all trips: Out << "Total time: " << setw(5) << totalHours; Out << ":"; Out << setw(2) << setfill('0') << totalMinutes << endl; Out << setfill(' '); Out << "Average MPH: " << setw(8) << setprecision(2) << overallMPH << endl; }