// Please i need this answer with full proper explonation //
(Gas Mileage) Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several trips by recording miles driven and gallons used for each trip. Develop a C++ program that uses a while statement to input the miles driven and gallons used for each trip. The program should calculate and display the miles per gallon obtained for each trip and print the combined miles per gallon obtained for all tankfuls up to this point.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<float>milPerGal;//vedtor to store all computed miles per gallon
float miles, gallons;
float tmp;
do
{
cout << "Please, input the miles(-1 to exit): ";
cin >> miles;
if (miles == -1)break;
cout << "Please, input the gallons: ";
cin >> gallons;
tmp = miles / gallons;
cout << "For this trip obtained " << tmp << " miles per gallon " << endl;
milPerGal.push_back(tmp);
cout << "Combined miles per gallon obtained for all tankfuls up to this point:\n";
vector<float>::iterator it;
for (it = milPerGal.begin(); it != milPerGal.end(); it++)
{
cout << *it << endl;
}
} while (true);
}
Comments
Leave a comment