Script started on Fri Sep 10 14:43:45 1999 strauss[2:43pm] [~/Class/cisc181/examples/]> cat 03-miles-per-gallon.cc // 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; } strauss[2:43pm] [~/Class/cisc181/examples/]> CC 03-,miles-per-gallon.cc strauss[2:44pm] [~/Class/cisc181/examples/]> a.out Enter the gallons used (-1 to end): 5 Enter the miles driven: 23 The miles / gallon for this tank was 4.6 Enter the gallons used (-1 to end): 20 Enter the miles driven: 400 The miles / gallon for this tank was 20 Enter the gallons used (-1 to end): 17 Enter the miles driven: 36.4 The miles / gallon for this tank was 2.14118 Enter the gallons used (-1 to end): 0 ERROR: gallons = 0, please try again Enter the gallons used (-1 to end): -1 The ovrall average miles / gallon was 10.9381 strauss[2:45pm] [~/Class/cisc181/examples/]> strauss[2:45pm] [~/Class/cisc181/examples/]> a.out Enter the gallons used (-1 to end): -1 strauss[2:45pm] [~/Class/cisc181/examples/]> a.out Enter the gallons used (-1 to end): 20 Enter the miles driven: 400 The miles / gallon for this tank was 20 Enter the gallons used (-1 to end): 10.3 Enter the miles driven: 200 The miles / gallon for this tank was 19.4175 Enter the gallons used (-1 to end): -1 The ovrall average miles / gallon was 19.802 strauss[2:46pm] [~/Class/cisc181/examples/]> exit strauss[2:46pm] [~/Class/cisc181/examples/]> exit script done on Fri Sep 10 14:46:32 1999