Write a C Program for the following in Visual Studio Drivers

Write a C++ Program for the following in Visual Studio:

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

Notes: The program must be fully documented or commented. Also, use meaningful prompt so that the user won\'t guess what to input.

Solution

Please follow the code and comments for description :

CODE :

#include <iostream> // required header files

using namespace std;

int main() // driver method
{
double gallonsUsed = 0, avgMilesPerGallon = 0, totMilesPerGallon = 0, milesPerGallon = 0, milesDriven = 0; // required initialisations
int count = 0;
  
while (gallonsUsed != -1) { // iterating over the loop till the user enters -1 to exit
  
cout << \"Enter the gallons used (-1 to end) : \"; // getting the data from the user with a prompt
cin >> gallonsUsed; // saving tha data to a variable
  
if(gallonsUsed == -1) { // checking for the input if twas a exit condition
break; // breaking the loop
}
  
count++; // incrementing the count dynamically for the user entered data
  
cout << \"Enter the miles driven : \"; // prompt to enter the data from the user
cin >> milesDriven; // saving the data to a variable
  
milesPerGallon = milesDriven / gallonsUsed; // calculating the mileage for the truck details entered
cout << \"The Miles / Gallon for this tank was \" << milesPerGallon << endl; // print the data to console
  
totMilesPerGallon = totMilesPerGallon + milesPerGallon; // calculating the total miles per gallon for the user entries
  
}
avgMilesPerGallon = totMilesPerGallon / count; // calculating the average miles per gallon for the user entries
cout << \"The overall average Miles/Gallon was \" << avgMilesPerGallon << endl; // print the data to console
  
return 0;
}


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


Hope this is helpful.

Write a C++ Program for the following in Visual Studio: Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of sever
Write a C++ Program for the following in Visual Studio: Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of sever

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site