Overclock.net banner
1 - 15 of 15 Posts

·
Registered
Joined
·
139 Posts
Discussion Starter · #1 ·
I got help from you guys before and figured I'd come back since I'm stuck again. I have to make this program:
Write, compile, debug, and run a program that will calculate gasoline mileage for a trip consisting of multiple parts. For each part of a trip, it will prompt for and accept as input an odometer reading (if you are not sure what this is, please ask or look it up) and number of gallons of gasoline, and will calculate miles driven and the mpg (miles per gallon) for that part and print out all 4 values in row of a table.
Assume that the odometer reading before the start of the trip was 0.
Display all doubles with 2 decimal places.
Your program will continue to request input until the user types in 0 for the odometer.
You will have to calculate the miles driven during each part! How?
After all input is done, print the length of the trip, and calculate and print out Trip MPG, which is the total mileage for the whole trip divided by the total gallons for the whole trip. (Note: the trip MPG is NOT the average of all the partial MPG's!)
Code Header input/output:
List the input of 3 specific odometer readings and 3 specific number of gallons, list expected miles driven and mpg for each. Also list the Trip MPG.
Note: You will have prompts and input in between the output lines of the table. That's okay! Have the output table display over to the right so that it looks better.
Here is an example of the output:
Odometer Gallons Miles MPG
(some prompts and input)
321 20 321 16.05
(some prompts and input)
543 13 222 17.08
(some prompts and input)
601 3 58 19.34
(some prompts and input)
774 13 173 13.31
This trip was 774 miles. You used 49 gallons.
Your mileage for this trip was: 15.80 miles per gallon.

The code I have so far is follows:

int main ( )
{

double totalodometer, prevodometer, MPG, gallons;
prevodometer = 0;
cout << "\t\t\tOdometer Gallons Miles Driven MPG" << endl
<< "\t\t\t

" << endl;
while( totalodometer != 0)
{
cout << "Enter odometer reading " << endl;
cin >> totalodometer;
if (totalodometer == 0)
break;
else
cout << "Enter gallons used: " << endl;
cin >> gallons;
prevodometer = totalodometer - prevodometer;
MPG = prevodometer/gallons;
cout << "\t\t\t " << totalodometer << "\t " << gallons << "\t " << prevodometer << "\t " << MPG << endl;

}
system("PAUSE");
return 0;
}
When I get to the third input which would be 601 and 3, it screws up because it's subtracting 222 and not 543. Is there any way to fix this so it works properly?

Edit: Sorry for the formatting. Is there a better way to format?
 

·
Premium Member
Joined
·
7,733 Posts
A better way to format your post would be to use the code tool

Code:

Code:
int main ( )
{

double totalodometer, prevodometer, MPG, gallons;
prevodometer = 0;
cout << "\t\t\tOdometer Gallons Miles Driven MPG" << endl
<< "\t\t\t
" << endl;
while( totalodometer != 0)
{
cout << "Enter odometer reading " << endl;
cin >> totalodometer;
if (totalodometer == 0)
break;
else
cout << "Enter gallons used: " << endl;
cin >> gallons;
prevodometer = totalodometer - prevodometer;
MPG = prevodometer/gallons;
cout << "\t\t\t " << totalodometer << "\t " << gallons << "\t " << prevodometer << "\t " << MPG << endl;

}
system("PAUSE");
return 0;
}
Just in case you didn't know you could do that
smile.gif
 
  • Rep+
Reactions: Damaging Excess

·
Registered
Joined
·
139 Posts
Discussion Starter · #4 ·
Quote:
Originally Posted by Crooksy View Post

A better way to format your post would be to use the code tool

Code:

Code:
int main ( )
{

double totalodometer, prevodometer, MPG, gallons;
prevodometer = 0;
cout << "\t\t\tOdometer Gallons Miles Driven MPG" << endl
<< "\t\t\t
" << endl;
while( totalodometer != 0)
{
cout << "Enter odometer reading " << endl;
cin >> totalodometer;
if (totalodometer == 0)
break;
else
cout << "Enter gallons used: " << endl;
cin >> gallons;
prevodometer = totalodometer - prevodometer;
MPG = prevodometer/gallons;
cout << "\t\t\t " << totalodometer << "\t " << gallons << "\t " << prevodometer << "\t " << MPG << endl;

}
system("PAUSE");
return 0;
}
Just in case you didn't know you could do that
smile.gif
I did not know how to do that. Thank you
biggrin.gif
 

·
Registered
Joined
·
139 Posts
Discussion Starter · #5 ·
Quote:
Originally Posted by Luminouslight View Post

I don't think this is the problem, but you declare totalodometer and use it before you actually initialize it.
I did that so the while loop would start no matter what because we're forced into using a while loop instead of a do while which would have been the preferred choice
 

·
Registered
Joined
·
139 Posts
Discussion Starter · #7 ·
Quote:
Originally Posted by Luminouslight View Post

Well, prevodometer from the looks of the code is actually the mileage between odometer readings, not the previous odometer reading. Not entirely sure if that is what you wanted.
I know. I just don't know how to go about wording it so it actually takes the previous entery and subtracts it from the new entry given they're the same variable
 

·
Registered
Joined
·
407 Posts
Quote:
Originally Posted by Damaging Excess View Post

I know. I just don't know how to go about wording it so it actually takes the previous entery and subtracts it from the new entry given they're the same variable
Your previous total odometer is going to be the next prevodometer. You shouldn't need to subtract anything from the total to find it. Just make sure to keep it for the next iteration of the loop.

Separate your mileage in a different variable. or just

Code:

Code:
MPG = (totalodometer-prevoodometer)/gallons
EDIT:
This should probably be what you are looking for.

Code:

Code:
int main ( )
{

double totalodometer, prevodometer, MPG, gallons;
prevodometer = 0;
cout << "\t\t\tOdometer Gallons Miles Driven MPG" << endl
<< "\t\t\t
" << endl;
while( totalodometer != 0)
{
cout << "Enter odometer reading " << endl;
cin >> totalodometer;
if (totalodometer == 0)
break;
else
cout << "Enter gallons used: " << endl;
cin >> gallons;
MPG = (totalodometer-prevodometer)/gallons;

cout << "\t\t\t " << totalodometer << "\t " << gallons << "\t " << prevodometer << "\t " << MPG << endl;

prevodometer = totalodometer;

}
system("PAUSE");
return 0;
}
 

·
Registered
Joined
·
139 Posts
Discussion Starter · #9 ·
Quote:
Originally Posted by Luminouslight View Post

Your previous total odometer is going to be the next prevodometer. You shouldn't need to subtract anything from the total to find it. Just make sure to keep it for the next iteration of the loop.

Separate your mileage in a different variable. or just

Code:

Code:
MPG = (totalodometer-prevoodometer)/gallons
EDIT:
This should probably be what you are looking for.

Code:

Code:
int main ( )
{

double totalodometer, prevodometer, MPG, gallons;
prevodometer = 0;
cout << "\t\t\tOdometer Gallons Miles Driven MPG" << endl
<< "\t\t\t
" << endl;
while( totalodometer != 0)
{
cout << "Enter odometer reading " << endl;
cin >> totalodometer;
if (totalodometer == 0)
break;
else
cout << "Enter gallons used: " << endl;
cin >> gallons;
MPG = (totalodometer-prevodometer)/gallons;

cout << "\t\t\t " << totalodometer << "\t " << gallons << "\t " << prevodometer << "\t " << MPG << endl;

prevodometer = totalodometer;

}
system("PAUSE");
return 0;
}
Not quite. It fixed the mpg but now the miles driven isn't being displayed right. It's just taking the previous entry and not the difference between the old entry and the new one
 

·
Registered
Joined
·
743 Posts
All you have to do is add

Code:

Code:
prevodometer = totalodometer;
Code:

Code:
while( totalodometer != 0)
{
        cout << "Enter odometer reading " << endl;
        cin >> totalodometer;
        if (totalodometer == 0)
                break;
        else
                cout << "Enter gallons used: " << endl;
                cin >> gallons;
        prevodometer = totalodometer - prevodometer;
        MPG = prevodometer/gallons;
        cout << "\t\t\t " << totalodometer << "\t " << gallons << "\t " << prevodometer << "\t " << MPG << endl;
        prevodometer = totalodometer;
}
You are changing prevodometer and not setting it back to totalodometer.
 

·
Registered
Joined
·
1,642 Posts
this code should do what you want.
I can't really find a way though to not make the while condition worthless (because it is always true now with the break).

Code:

Code:
int main ( )
{
        double totalodometer, prevodometer, MPG, gallons;
        totalodometer = 1.0;
        prevodometer = 0;
        cout << "\t\t\tOdometer Gallons Miles Driven MPG" << endl
        << "\t\t\t
        " << endl;
        while(totalodometer != 0)
        {
                cout << "Enter odometer reading " << endl;
                cin >> totalodometer;
                if (totalodometer == 0)
                        break;
                cout << "Enter gallons used: " << endl;
                cin >> gallons;
                MPG = prevodometer/gallons;
                cout << "\t\t\t " << totalodometer << "\t " << gallons << "\t " << (totalodometer - prevodometer) << "\t " << MPG << endl;
                prevodometer = totalodometer;
        }
        system("PAUSE");
        return 0;
}
 

·
Registered
Joined
·
139 Posts
Discussion Starter · #12 ·
Quote:
Originally Posted by FlyingNugget View Post

All you have to do is add

Code:

Code:
prevodometer = totalodometer;
Code:

Code:
while( totalodometer != 0)
{
        cout << "Enter odometer reading " << endl;
        cin >> totalodometer;
        if (totalodometer == 0)
                break;
        else
                cout << "Enter gallons used: " << endl;
                cin >> gallons;
        prevodometer = totalodometer - prevodometer;
        MPG = prevodometer/gallons;
        cout << "\t\t\t " << totalodometer << "\t " << gallons << "\t " << prevodometer << "\t " << MPG << endl;
        prevodometer = totalodometer;
}
You are changing prevodometer and not setting it back to totalodometer.
Now I just feel dumb haha. Thank you so much. +rep for you all
 

·
Registered
Joined
·
407 Posts
Oh... Well then just make a new variable mileage. I thought that the previous odometer was supposed to be there because that was the variable name

Code:

Code:
int main ( )
{

double totalodometer, prevodometer, MPG, gallons, mileage;
prevodometer = 0;
cout << "\t\t\tOdometer Gallons Miles Driven MPG" << endl
<< "\t\t\t
" << endl;
while( totalodometer != 0)
{
cout << "Enter odometer reading " << endl;
cin >> totalodometer;
if (totalodometer == 0)
break;
else
cout << "Enter gallons used: " << endl;
cin >> gallons;
mileage = totalodometer-prevodometer
MPG = (mileage)/gallons;

cout << "\t\t\t " << totalodometer << "\t " << gallons << "\t " << mileage << "\t " << MPG << endl;

prevodometer = totalodometer;

}
system("PAUSE");
return 0;
}
 

·
Registered
Joined
·
139 Posts
Discussion Starter · #15 ·
Quote:
Originally Posted by Luminouslight View Post

Oh... Well then just make a new variable mileage. I thought that the previous odometer was supposed to be there because that was the variable name

Code:

Code:
int main ( )
{

double totalodometer, prevodometer, MPG, gallons, mileage;
prevodometer = 0;
cout << "\t\t\tOdometer Gallons Miles Driven MPG" << endl
<< "\t\t\t
" << endl;
while( totalodometer != 0)
{
cout << "Enter odometer reading " << endl;
cin >> totalodometer;
if (totalodometer == 0)
break;
else
cout << "Enter gallons used: " << endl;
cin >> gallons;
mileage = totalodometer-prevodometer
MPG = (mileage)/gallons;

cout << "\t\t\t " << totalodometer << "\t " << gallons << "\t " << mileage << "\t " << MPG << endl;

prevodometer = totalodometer;

}
system("PAUSE");
return 0;
}
FlyingNugget beat you too it. Thank you so much for the help though regardless
 
1 - 15 of 15 Posts
Top