// Example Program for CS 1044 Notes // // Student: // // Programmer: William D McQuain // OS: Windows NT Workstation 4.0 // System: Pentium II 400, 256 MB Memory // Compiler: Visual C++ 6.0, Service Pack 4 // Last modified: July 10, 2001 // // Purpose // This program reads data for one trip, including origin, // destination, distance and time. The program converts the // trip time to minutes and computes the average MPH for the // trip. // // 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 int main() { const int MINPERHOUR = 60; // Number of minutes in an hour ifstream In("TripData.txt"); // 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... << "TripData.txt" // including the file name. << endl // Bang "return" << "Exiting now..." << endl; // Finish the message. exit(1); // Terminate a failed execution. } string tripOrigin, // Name of starting point for trip. tripDestination; // Name of destination for trip. int tripMiles; // Length of trip in miles. // Time trip took is given as hh:mm so: int tripHours, // hours field tripMinutes, // minutes field tripTime; // Time for trip in minutes. double tripMPH; // Speed of trip, in miles per hour. // Read the input data from the input stream: // In.ignore(INT_MAX, '\n'); // Skip over the two header lines in the In.ignore(INT_MAX, '\n'); // input file. getline(In, tripOrigin, '\t'); // Read: the name of the trip origin. getline(In, tripDestination, '\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 // Calculate the total minutes for this trip: tripTime = MINPERHOUR * tripHours + tripMinutes; // Calculate the average MPH for this trip: tripMPH = MINPERHOUR * double(tripMiles) / double(tripTime); ofstream Out("Summary.txt"); // Attach an output stream to the output file. Out << fixed << showpoint; // Prepare output stream for decimal output. // Write identification information to output file: Out << "Programmer: " << "Bill McQuain" << endl; Out << "CS 1044 Notes Example" << endl; Out << endl; // Column headers and delimiter for top and bottom of output table: const string HEADERS = "Origin Destination Mileage Minutes MPH"; const string DELIMITER = "-----------------------------------------------------------------"; // Write output column headers and table delimiter to output file: Out << HEADERS << endl; Out << DELIMITER << endl; // Write the results for this trip: Out << left; // left-justify names Out << setw(20) << tripOrigin; // in 20-column fields Out << setw(20) << tripDestination; Out << right; // right-justify numbers Out << setw(7) << tripMiles // in varied-width fields << setw(10) << tripTime << setw(8) << setprecision(1) << tripMPH // write MPH to 1 decimal place << endl; // Write a delimiter for the bottom of the output table: Out << DELIMITER << endl; // Close the input and output files: In.close(); Out.close(); // Terminate a successful execution: return 0; }