loops

Any idea why the while loop exits the program rather then continue on to the coomand after it is finished ?


//Kennith
//06/30/2017
//Fuel Cost calculater
//Program calculates cost of fuel


#include<iostream>
#include <iomanip>

using namespace std;

int main()

{
int beginningRead;
int endRead;
int gallonsReq;

beginningRead = 0;
endRead = 0;
gallonsReq = 0;
int numberTrips = 1;

cout << "Enter the reading on the odometer at the beginning of your trip" << endl;
cin >> beginningRead;

cout << " Enter the reading on the odometer at the end of your trip";
cin >> endRead;

cout << "Enter the gallons required to fill the tank";
cin >> gallonsReq;



cout << "do you wish to enter more trips? 1 for yes";
cin >> numberTrips;

while (numberTrips == 1)
{
cout << "Enter the reading on the odometer at the beginning of your trip" << endl;
cin >> beginningRead;

cout << " Enter the reading on the odometer at the end of your trip";
cin >> endRead;

cout << "Enter the gallons required to fill the tank";
cin >> gallonsReq;
numberTrips++;

cout << "do you wish to enter more trips? 1 for yes, 0 for no";
cin >> numberTrips;
if (numberTrips == 0)
{
break;
}
}
int ppg;
int count;
for (count = 0; count < numberTrips; count++)
{
cout << " What is the price per gallon at station " << count + 1 << " $";
cin >> ppg;
while (ppg <= 0)
{
cout << "error enter a different price";
cin >> ppg;
}
}
int averageMpg;
int averagePpg;
int mpd;

averageMpg = (endRead - beginningRead)*gallonsReq;
averagePpg = (endRead - beginningRead)*ppg;
mpd = (averagePpg / averageMpg);

cout << "Your cars average miles per gallon is" << averageMpg;
cout << " You bought gas at an average of $" << averagePpg;
cout << "The cost for 1 mile is." << mpd;

return 0;
}

Last edited on
Please use code tags with [ code ] your code here [ /code ]
Last edited on
any idea why the while loop exits the program rather then continue on to the coomand after it is finished ?

Are you sure the console window is not simply closing after executing your summary?
http://www.cplusplus.com/forum/beginner/1988/

You have some logic errors:
Line 48: What is the point of incrementing numberTrips here, when you overlay it at line 51.

Line 59: numberTrips will never be anything other than 0. That's how you broke out of your loop at line 54.

Line 57: ppg is uninitialized.

Line 61-62: You will never prompt for ppg because your for loop never executes.

Line 74-75: Your calculation of averagePpg and mpd is invalid because ppg was never initialized.

Lines 73-74: Your calculations will only use the last values of endRead, beginningRead and gallonsRequired. You want to calculate the total miles traveled and total gallons inside your while loop, then use those totals to calculate averageMpg and averagePpg.

Line 73: Your calculation of averageMpg is incorrect. You want to DIVIDE totalMiles by totalGallons.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.



Last edited on
Topic archived. No new replies allowed.