// Programmer: Gregory A. Perkins // Class: CS 66 // Due: 30 January 1997 // This program accepts odometer readings and amounts of fuel used on a // multi-leg trip. It displays the leg data (miles & mpg), and the trip // data (mpg) for each leg. When the user has completed data entry, the // program displays the total miles for the trip, the total gallons // consumed on the trip, and the average mpg for the trip. #include#include //Function Prototypes void welcome(double& oldOdom); void displayStopNum(); void getNewData(double& newGal, double& newOdom); double legMiles(double& newOdom, double oldOdom); double legMpg(double mil, double newGal); double tripMpg(double totalMil, double totalGal); void displayTripSummary(double totalMil, double totalGal, double aveMpg); //Function Definitions //----------------------------------------------------------------- int main() { double oldOdometer = 0; welcome(oldOdometer); double newGallons = 0; double newOdometer = 0; double miles = 0; double mpg = 0; double totalMiles = 0; double totalGallons = 0; do { displayStopNum(); getNewData(newGallons, newOdometer); if (newGallons > 0) { cout << "Distance traveled this leg: "; miles = legMiles(newOdometer, oldOdometer); cout << setprecision(5) << miles << endl << "MPG for this leg: "; mpg = legMpg(miles, newGallons); cout << setprecision(3) << mpg << endl; totalMiles += miles; totalGallons += newGallons; cout << "MPG for trip: "; mpg = tripMpg(totalMiles, totalGallons); cout << setprecision(3) << mpg << endl << endl; oldOdometer = newOdometer; } } while (newGallons > 0); displayTripSummary(totalMiles, totalGallons, mpg); return 0; } //----------------------------------------------------------------- void welcome(double& oldOdom) { cout << "Welcome to the MPG wizard." << endl << endl << "Please enter the initial odometer reading: "; cin >> oldOdom; // initial odometer reading cout << endl; return; } //----------------------------------------------------------------- void displayStopNum() { static int stopNumber = 0; // keep track of the current stop number stopNumber += 1; // increase stop number by one every time function is called cout << "Fuel Stop #" << stopNumber << endl << "-------------" << endl; } //----------------------------------------------------------------- void getNewData(double& newGal, double& newOdom) { cout << "Input gallons consumed (0 to quit): "; cin >> newGal; if (newGal > 0) // get new odometer reading if trip is not over { cout << "Input new odometer reading: "; cin >> newOdom; } cout << endl; } //----------------------------------------------------------------- double legMiles(double& newOdom, double oldOdom) { if (newOdom < oldOdom) newOdom += 1000000.0; return (newOdom - oldOdom); } //----------------------------------------------------------------- double legMpg(double mil, double newGal) { return (mil / newGal); } //----------------------------------------------------------------- double tripMpg(double totalMil, double totalGal) { return (totalMil / totalGal); } //----------------------------------------------------------------- void displayTripSummary(double totalMil, double totalGal, double aveMpg) { cout << "Trip Summary" << endl << "-------------" << endl << "Total distance traveled: " << setprecision(5) << totalMil << endl << "Total fuel consumed: " << setprecision(3) << totalGal << endl << endl << "Average MPG: " << setprecision(3) << aveMpg << endl; }