Write a C Program for the following Drivers are concerned wi
Write a C++ Program for the following:
Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankful\'s 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 tankful\'s.
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
PLEASE FIX PROBLEM ON LINE 2, doesnt contain a closing }
#include<stdio.h>
int main(void)
{
float gallons, miles, totalGallons = 0.0;
float totalAverage = 0.0;
float totalMiles = 0.0;
printf (\"Enter gallons used (-1 to end): \");
scanf (\"%f\", &gallons);
while (gallons != -1)
{
totalGallons += gallons;
printf (\"Enter number of miles driven: \");
scanf (\"%f\",&miles);
totalMiles += miles;
printf (\"The miles per gallon for this tank was %f\", miles/gallons);
printf (\"\ Enter the gallons used (-1 to end):\");
scanf (\"%f\", &gallons);
}
if (totalGallons != 0)
{
/* Calculate total average as totalMiles/totalGallons*/
totalAverage = totalMiles/totalGallons;
/*Print totalAverage to screen*/
printf (\"\ The overall average miles per gallon was %f \ \", totalAverage);
}
else
printf (\"No gallons were entered\ \");
return 0;
}
Solution
#include <iostream>
 using namespace std;
 int main(void)
 {
 float gallons, miles, totalGallons = 0.0;
 float totalAverage = 0.0;
 float totalMiles = 0.0;
 cout<<\"Enter gallons used (-1 to end): \";
 cin>>gallons;
 while (gallons != -1)
 {
 totalGallons += gallons;
 cout<<\"Enter number of miles driven: \";
 cin>>miles;
 totalMiles += miles;
 cout<<\"The miles per gallon for this tank was:\"<<miles/gallons;
cout<<\"\ Enter the gallons used (-1 to end):\";
 cin>>gallons;
 }
 if (totalGallons != 0)
 {
 /* Calculate total average as totalMiles/totalGallons*/
 totalAverage = totalMiles/totalGallons;
 /*Print totalAverage to screen*/
 cout<<\"\  The overall average miles per gallon was\"<< totalAverage<<endl;
 }
 else
 cout<<\"No gallons were entered\"<<endl;
 return 0;
 }


