Answer to Question #328954 in C++ for farzii

Question #328954

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


1
Expert's answer
2022-04-15T04:43:44-0400
#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);
}



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS