This program is suppose to display MPG's (total), and the combined MPG of all the total MPG's you input.

// Exercise 4. 12: ex04_12.cpp
 #include <iostream>
 using namespace std;

 int main()
 {
     double mpg;
     double gallons;
     double combinedMpg = 1;
     double total;


while ( mpg != -1 )
    {
        cout << "\nEnter miles (Enter -1 to quit): ";
        cin >> mpg;

        if ( mpg != -1)
        {
            cout << "Enter gallons: ";
            cin >> gallons;
        }
            cout << "\nMPG this tankful: " << (total = (mpg / gallons)) << endl;
            cout << "Total MPG: " << (combinedMpg = total / combinedMpg ) << endl;
    }
 }

Any help?
Output should be along these lines...I just switched some of the words.

Enter the miles used (-1 to quit): 287
Enter gal l ons: 13
MPG this tankful : 22. 076923
Total MPG: 22. 076923

Enter the miles used (-1 to quit): 200
Enter gal l ons: 10
MPG this tankful : 20. 000000
Total MPG: 21. 173913

Enter the miles used (-1 to quit): 120
Enter gal l ons: 5
MPG this tankful : 24. 000000
Total MPG: 21. 678571

Recommended Answers

All 3 Replies

First, the code would be clearer if your variable names better expressed what they were.

What's "total"? The input of distance driven should be "miles", not "mpg". mpg = miles / gallons better expresses what you're doing.

The calculation of the mpg for a given tankful should occur inside the if condition - when the miles input is -1, you don't want to do a calculation, as would happen in your code.

For the overall mpg value, simply averaging the individual mpg calculations will not be accurate. And you're not doing that correctly. (Look at your code, you're saying overall_mpg = current_mpg / overall_mpg - does that make any sense?) You should keep a running total of miles driven and gallons used, and calculate the overall mpg with those totals.

Is it in the assignment to display the running overall mpg after each entry, or are you to display the summary after all input is concluded? If the latter case, the calculation and display of overall mpg should occur outside the loop.

First, the code would be clearer if your variable names better expressed what they were.

What's "total"? The input of distance driven should be "miles", not "mpg". mpg = miles / gallons better expresses what you're doing.

The calculation of the mpg for a given tankful should occur inside the if condition - when the miles input is -1, you don't want to do a calculation, as would happen in your code.

For the overall mpg value, simply averaging the individual mpg calculations will not be accurate. And you're not doing that correctly. (Look at your code, you're saying overall_mpg = current_mpg / overall_mpg - does that make any sense?) You should keep a running total of miles driven and gallons used, and calculate the overall mpg with those totals.

Is it in the assignment to display the running overall mpg after each entry, or are you to display the summary after all input is concluded? If the latter case, the calculation and display of overall mpg should occur outside the loop.

haha yeah that was a dumb variable, since MPG is = miles per gallon.

ok well the assignment was to display the MPG of the entered tankful AND the average of all MPG's entered.

still couldnt get it. I revised it and this is where i'm at.

#include <iostream>
 using namespace std;

 int main()
 {
     double miles;
     double gallons;
     double combinedMpg = 1;
     double mpg;
     double averageMpgs;

while ( mpg != -1 )
    {
        cout << "\nEnter miles (Enter -1 to quit): ";
        cin >> miles;

        if ( miles != -1)
        {
            cout << "Enter gallons: ";
            cin >> gallons;
            cout << "\nMPG this tankful: " << (mpg = (miles / gallons)) << endl;
            cout << "Total MPG: " << (averageMpgs = mpg / combinedMpg ) << endl;
        }
        averageMpgs += combinedMpg;
    }
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.