Sunday, August 2, 2009

Question about C++. Trying to write a program that configures miles driven and miles per gallon.?

Input information is gallons used, starting mileage, and ending mileage. I need to configure the miles driven and the miles per gallon.





My problem is coming in because I need to be able to do this in a loop. Here is the code I have so far:





do


{


cout %26lt;%26lt; "Enter the gallons used (Enter -1 to End): ";


cin %26gt;%26gt; gallonsUsed;


totalGallonsUsed = (totalGallonsUsed + gallonsUsed);





cout %26lt;%26lt; "Enter the ending mileage: ";


cin %26gt;%26gt; endingMileage;





milesDriven = (endingMileage - milesDriven);


cout %26lt;%26lt; "Miles driven: " %26lt;%26lt; milesDriven %26lt;%26lt; endl;





totalMilesDriven = totalMilesDriven + milesDriven;





milesPerGallon = (milesDriven / gallonsUsed);


cout %26lt;%26lt; "The miles/gallon for this tank was: " %26lt;%26lt; milesPerGallon;





overallMilesPerGallon = (totalMilesDriven / totalGallonsUsed);

Question about C++. Trying to write a program that configures miles driven and miles per gallon.?
Have a do ... while loop like this





cout %26lt;%26lt; "Enter the gallons used (Enter -1 to End): ";


cin %26gt;%26gt; gallonsUsed;





do


{


///////// Calculation Code Goes here


totalGallonsUsed = (totalGallonsUsed + gallonsUsed);





cout %26lt;%26lt; "Enter the ending mileage: ";


cin %26gt;%26gt; endingMileage;








////////////////////////////////


/// ask again for input


//////////////////////////////


cout %26lt;%26lt; "Enter the gallons used (Enter -1 to End): ";


cin %26gt;%26gt; gallonsUsed;


totalGallonsUsed = (totalGallonsUsed + gallonsUsed);


} while(gallonsUsed%26gt;0)
Reply:Check these points:


%26gt;%26gt; Initialized totalGallonsUsed, totalMilesDriven.


%26gt;%26gt; Condition given for while should be while(gallonsUsed!=-1).





This should solve your problem.

liama-song

No comments:

Post a Comment