Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Drivers are concerned with the mileage obtained by their automobiles. One driver

ID: 3661384 • Letter: D

Question

Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. Develop a C++ program that will input the miles driven and gallons used for each tankful. The program should calculate and display the miles per gallon obtained for each tankful. After processing all input information, the program should calculate and print the combined miles per gallon obtained for all tankfuls.

Output
Enter the gallons used (-1 to end): 16
Enter the miles driven: 220
The Miles / Gallon for this tank was 13.75
Enter the gallons used (-1 to end): 16.5
Enter the miles driven: 272
The Miles / Gallon for this tank was 16.4848
Enter the gallons used (-1 to end): -1
The overall average Miles/Gallon was 15.1385

- This bottom code is what I had, but I am having trouble inputing the second number of gallons used. The command promt just continously creates more wording and does not allow for an input. What could be the fix or a code that works? Also it would be nice if there was some explanation to each part of the code.

//Header file section
#include<iostream>
using namespace std;
void main()
{
   float miles, gallons, MPG, totMiles = 0, totGallons = 0;
   int i = 1;
   //inputting miles
   cout << "Enter mileage for trip:#" << i << " :";
   cin >> miles;
   //inputting gallons
   cout << "Enter galllons for trip:#" << i << " :";
   cin >> gallons;
   do
   {
       MPG = miles / gallons;
       totMiles += miles;
       totGallons += gallons;
       cout << "MPG for trip " << i << " :" << MPG << end1;
       cout << " MPG for all trips so far is : "
           << totalMiles / totGallons << end1;
       i++;
       //inputting miles
       cout << "Enter mileage for trip:#" << i << " :";
       cin >> miles;
       if (miles == -1)
           break;
       //inputting gallons
       cout << "Enter gallons for trip:#" << i << " :";
       cin >> gallons;

   } while (miles != -1);
   cout << "Thanks for using our MPG program!" << end1;
}//end main

Explanation / Answer

Please find below the required program as per the sample output provided.

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
float miles, gallons, MPG, totMiles = 0, totGallons = 0;
//inputting gallons
cout << "Enter the gallons used (-1 to end): ";
cin >> gallons;
if (gallons == -1) // breaking condition
{
cout<<" The overall average Miles/Gallon is undefined";
exit(0);
}
//inputting miles
cout <<" Enter the miles driven: ";
cin >> miles;
while(1)//infinite loop
{
MPG = miles / gallons;
totMiles += miles;
totGallons += gallons;
cout <<" The Miles / Gallon for this tank was " << MPG ;//MPG per entry
//inputting gallons
cout <<" Enter the gallons used (-1 to end): ";
cin >> gallons;
if (gallons == -1) // breaking condition
break;
//inputting miles
cout <<" Enter the miles driven: ";
cin >> miles;

}
cout <<" The overall average Miles/Gallon was "<<totMiles/totGallons ;//cumulative average
return 0;
}//end main

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote