Heres the question:
Drivers worried with mileage obtained by automobiles.1 driver kept track of all tanks of gas by recording miles and gallons used for tanks. Develop a C++ program that uses while loop to input the miles %26amp; gallons used for each tankful. Program should calculate%26amp;display the miles/gallon for each tankful. The program should calculate average miles per gallon obtained for all tankfuls
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
double gallon;
double avrge;
double mile;
double milegallon;
counter=0;
cout%26lt;%26lt;"enter gallons used in tank( or 0 to quit) "%26lt;%26lt;endl;
cin%26gt;%26gt;gallon;
counter++;
while(gallon != 0)
{
cout%26lt;%26lt;"Please enter distance in miles"%26lt;%26lt;endl;
cin%26gt;%26gt;mile;
milegallon = mile/gallons;
cout%26lt;%26lt;"Mile per gallon is:"%26lt;%26lt;milegallon%26lt;%26lt;endl;
}
avrge = milegallon / counter;
cout%26lt;%26lt;"Overall average was"%26lt;%26lt;avrge%26lt;%26lt;endl;
return 0;
}
Is my counter correct?
Will 'milegallon' outside while loop have total miles per gallon from inside the loop or do I need to enter more info?
C++ Programming- Help Please e with a little problem I'm having with my program.?
You're close. Don't use a counter.
Instead, what you want to do is keep track of the total miles and total gallons. i.e. totMile += mile and totGal += gallon. When you get out of the loop divide the totMile by totGal to get the ave mpg.
Reply:That is going to be an endless loop. Before the closing } of the while loop you will need to ask how many gallons are used in the trip again. That way the user can say 0 to end the program.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment