# Thread: C++ Program Help (MPG calculator)

1. sorry i forgot to post it i guess

Code:
```//Gas
//september-27-05
//frfrf

#include <iostream>
using namespace std;

int main()
{
int liters = 0;
const double GALLONS_PER_LITER = 0.264179;
double mpg = distance / (liters*GALLONS_PER_LITER);
double distance = 0;

do
{
cout << "Please input how many liters of gasoline is in your vehicle";
cin >> liters;
cout << "Please input the distance in miles you traveled in your vehicle.";
cin >> distance;

cout << "Your vehicle's MPG is" << (distance/liters) << endl;

}while(liters >-1);

return 0;

}```
ok so say i turn double mpg = distance / (liters*GALLONS_PER_LITER);

into

double mpg = 0.0

then where would i put the distance / (liters*GALLONS_PER_LITER); ?

2. After you have the variables involved, of course.

You also need to save the result of the calculation. You can't just go
Code:
`distance / (liters*GALLONS_PER_LITER);`
but rather have to go
Code:
`mpg = distance / (liters*GALLONS_PER_LITER);`

3. then where would i put thYou are dividing distance by (liters*GALLONS_PER_LITER);?
Would it not make sense to do that after you have input the distance? Other wise you are just dividing 0 by something (which will always be zero).

-Use the answers in a calculation.
-display the results.

The value of your variable can be changed as many times as you want throughout your program, so don't get confused with thinking that you have to have the final result at the time of declaration.

Also, as stated, in C++ you do not have to declare your variables at the top of your code.

Code:
```#include <iostream>
using namespace std;

int main()
{
const double GALLONS_PER_LITER = 0.264179; //give a meaningful name to conversion constant

int liters = 0;         //will be set by cin before we use in calculation, but
double distance = 0.0;  //it's nice to get in the habit of initializing variables

double mpg = 0.0; //could (should) wait to declare this until closer to where we need it

do
{
cout << "Please input how many liters of gasoline is in your vehicle: ";
cin >> liters;

cout << "Please input the distance in miles you traveled in your vehicle: ";
cin >> distance;

//We've got our user input... let's do our calculation.
mpg = distance / (liters * GALLONS_PER_LITER);
// OR
//we could just as well declare mpg here and omit mpg lines above
//double mpg = distance / (liters * GALLONS_PER_LITER);

//display results
cout << "Your vehicle's MPG is: " << mpg << endl;

}while(liters >-1);

return 0;
}```

5. thank you everyone who helped!

-brent

6. Originally Posted by mrwooter
"A liter is 0.264179 gallons. Write a program that will read in the number of liters of gasoline consumed by the user's car and the number of miles traveled by the car, and will then output the number of miles per gallon the car delivered. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the number of miles per gallon. Your program should use a globally defines constant for the number of liters per gallon."
You need to make sure you read your assignment carefully, he wants a global constant that stores the 0.264179 conversion rate. Global means its defined outside of any function.

ie:
Code:
```#include <stdio.h>

const double foo = 0.264179; /* globally defined, accessible from
any function */

int main(void)
{
/* do things here */

return 0;
}```
Note: this is a C example obviously, but it shows the concept your teacher is looking for.