// Program calculates the miles per gallon // per tank of gas and overall given the // input data of miles driven and number // of gallons used for each tank // Exercise 2.16 from D&D #include main () { float gallons, total_gallons, miles, total_miles, mpg; // initialize total_gallons = 0; total_miles = 0; // initialize input (for sentinal value) cout << "Enter the gallons used (-1 to end): "; cin >> gallons; while (gallons != -1) { if (gallons == 0) { cout << "ERROR: gallons = 0, please try again"; cout << endl; } else { total_gallons += gallons; cout << "Enter the miles driven: "; cin >> miles; total_miles += miles; cout << "The miles / gallon for this tank was " << ( miles / gallons) << endl; cout << endl; } cout << "Enter the gallons used (-1 to end): "; cin >> gallons; } // Summation Phase if (total_gallons != 0) { cout << endl; cout << "The ovrall average miles / gallon was " << (total_miles / total_gallons) << endl; } return 0; }