1 /*C++ How to Program, 9/E, by Paul Deitel & Harvey Deitel.
3 Solution of exercise 4.13:
4 (Gas Mileage) Drivers are concerned with the mileage obtained by their
5 automobiles. One driver has kept track of several trips by recording miles
6 driven and gallons used for each trip. Develop a C++ program that uses a while
7 statement to input the miles driven and gallons used for each trip. The
8 program should calculate and display the miles per gallon obtained for each
9 trip and print the combined miles per gallon obtained for all tankfuls up to
12 Enter miles driven (-1 to quit): 287
13 Enter gallons used: 13
14 MPG this trip: 22.076923
17 Enter miles driven (-1 to quit): 200
18 Enter gallons used: 10
19 MPG this trip: 20.000000
22 Enter the miles driven (-1 to quit): 120
24 MPG this trip: 24.000000
27 Enter the miles used (-1 to quit): -1
29 Written by Juan Carlos Moreno (jcmhsoftware@gmail.com), 2023-01-16.*/
38 double miles
; //Miles driven for each trip
39 double gallons
; //Gallons used for each trip
40 double total_miles
= 0; //Miles driven for all trips
41 double total_gallons
= 0; //Gallons used for all trips
45 cout
<< "Enter miles driven (-1 to quit): ";
50 cout
<< "Enter gallons used: ";
52 total_gallons
+= gallons
;
54 cout
<< setprecision(6) << fixed
;
55 cout
<< "MPG this trip: " << miles
/gallons
<< endl
;
56 cout
<< "Total MPG: " << total_miles
/total_gallons