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

WHAT IS WRONG WITH THIS?

Solution

/*

this program was modified to c++ and error free

*/

#include <stdio.h>
#include <iostream>
using namespace std;

int main(void)
{
/*Initialize all the variables to zero*/
float gallons, miles, totalGallons = 0.0;
float totalAverage = 0.0;
float totalMiles = 0.0;

/* Prompt the user for number of gallons (user will enter –1 to end entries)*/
//printf (\"Enter gallons used (-1 to end): \");
cout<<\"Enter gallons used (-1 to end): \"<<endl;
cin>>gallons;
//scanf (\"%f\", &gallons);

/* While the number of gallons is not -1*/
while (gallons != -1)
{
/* Add number of gallons to totalGallons */
totalGallons += gallons;

/* Prompt user to enter number of miles driven */
//printf (\"Enter number of miles driven: \");
cout<<\"Enter number of miles driven: \";
cin>>miles;
//scanf (\"%f\",&miles);

/*Add number of miles driven to total number of miles driven*/
totalMiles += miles;

/* Calculate and print miles per gallon for current tank */
//printf (\"The miles per gallon for this tank was %.2f\", miles/gallons);
cout<<\"The miles per gallon for this tank was : \"<<miles/gallons<<endl;


/* Prompt user for number of gallons */
cout<<\"\ Enter the gallons used (-1 to end):\";
scanf (\"%f\", &gallons);
}
//cout<<\"Total gallons: \"<<totalGallons<<endl;
//printf (\"totalMiles: %.2f\",totalMiles);
//printf (\"Total gallons: %.2f\",totalGallons);

/* If totalGallons is not zero*/
if (totalGallons != 0)
{
/* Calculate total average as totalMiles/totalGallons*/
totalAverage = totalMiles/totalGallons;

/*Print totalAverage to screen*/
//printf (\"\ The overall average miles per gallon was %.2f \ \", totalAverage);
cout<<\"\ The overall average miles per gallon was :\ \"<<totalAverage<<endl;
}
else
/* If no gallons entered give error message to user*/
//printf (\"No gallons were entered\ \");
cout<<\"No gallons were entered\ \"<<endl;


//return 0;
getchar();
}

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
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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site