# C++ Program Help (MPG calculator)

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-27-2005
mrwooter
C++ Program Help (MPG calculator)
Hello, I have this project due today for class and I cant seem to get it compiled without errors, it gives me one error and its not specific enough for me to fix it on my own, so I am asking for help here. Here is the code, and what its "supposed" to do.

"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."

And here is my code;

Code:

```//Assignment 1 //Gas //september-27-05 // #include <iostream> using namespace std; int main() { int liters; double distance; double gallon = (0.264179*liters); 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 >0); return 0; }```
Thanks in advance to anyone who can help.

-Brent
• 09-27-2005
Speedy5
Code:

`double gallon = (0.264179*liters);`
You can't do that since liters is undefined at the point of usage. Basically what you want is have it as a constant ratio and then multiply it (factor-label) in the MPG equation:

Code:

```const double GALLONS_PER_LITER = 0.264179; ... double mpg = distance / (liters * GALLONS_PER_LITER);```
• 09-27-2005
FoodDude
int liters = 0;
• 09-27-2005
FoodDude
and what speedy said.
• 09-27-2005
mrwooter
Thanks speed and fooddude, i changed the int liters to equal 0, then i changed out the 2 lines that speedy gave me, and i seem to have more errors, i may have took what speedy said wrong and did the wrong things, this is my code as of now ;

Code:

```//Gas //september-27-05 // #include <iostream> using namespace std; int main() { int liters = 0; double mpg = distance / (liters * GALLONS_PER_LITER); const double GALLONS_PER_LITER = 0.264179; 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; return 0;```
and the errors now are;
C:\Documents and Settings\classics\Desktop\mpgproject.cpp(15) : error C2065: 'GALLONS_PER_LITER' : undeclared identifier
C:\Documents and Settings\classics\Desktop\mpgproject.cpp(15) : error C2563: mismatch in formal parameter list
C:\Documents and Settings\classics\Desktop\mpgproject.cpp(23) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type '' (or there is no acceptable conversion)
C:\Documents and Settings\classics\Desktop\mpgproject.cpp(25) : error C2563: mismatch in formal parameter list
C:\Documents and Settings\classics\Desktop\mpgproject.cpp(31) : fatal error C1004: unexpected end of file found
• 09-27-2005
spydoor
Pay attention to the errors.

You use GALLONS_PER_LITER in a calculation before you define it.

You never delare distance. (and, you probably don't want to use it in a calcualtion until after it's been input by the user)

Your do while loop has no closing brace... or a while()
• 09-27-2005
mrwooter
Im still confused about the gallons_per_liter error... I fixed everything else and now im only recieving 2 errors about the gallon_per_liter part.. heres my code and errors, if anyone knows a solution (explained in dummy terms for a C++ noob) it would be awesome and greatly appreciated.

Code:

```/Assignment 1 //Gas //september-27-05 //frfrf #include <iostream> using namespace std; int main() { int liters = 0; double mpg = distance / (liters*GALLONS_PER_LITER); const double GALLONS_PER_LITER = 0.264179; 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; }```

C:\Documents and Settings\classics\Desktop\mpgproject.cpp(14) : error C2065: 'GALLONSPERLITER' : undeclared identifier
C:\Documents and Settings\classics\Desktop\mpgproject.cpp(14) : error C2563: mismatch in formal parameter list
• 09-27-2005
dwks
Look here, these two lines are in the wrong order. You use GALLONS_PER_LITER and then you declare it:
Code:

```double mpg = distance / (liters*GALLONS_PER_LITER); const double GALLONS_PER_LITER = 0.264179;```
Put them the other way around.
• 09-27-2005
dwks
distance is also in the wrong place. And this line is a syntax error:
Code:

`/Assignment 1`
• 09-27-2005
Daved
The calculation of mpg should be moved much farther down. You must wait until the user enters the liters and the distance before you calculate the mpg. Follow the program flow from top to bottom on paper or in your head. You will see that it doesn't make sense as written.
• 09-27-2005
dwks
Yeah, right now it always calulates 0 litres.
• 09-27-2005
mrwooter
how would i move it down? i thought ints and const and doubles ect need to be determined at the top of the code?

i switched the 2 lines around and now only 1 error...

C:\Documents and Settings\classics\Desktop\mpgproject.cpp(15) : error C2563: mismatch in formal parameter list
• 09-27-2005
dwks
Quote:

i thought ints and const and doubles ect need to be determined at the top of the code?
In C, they do. In C++, they don't. But you want to declare the variables at the top of the function, and preform the calculation[s] on them later on.
• 09-27-2005
Daved
>> i thought ints and const and doubles ect need to be determined at the top of the code?

Nope, that is not true. There is a style of programming that declares these things at the top of code, but it is not appropriate for C++ anyway. If your instructor is telling you to do that, you can still assign a value later. Initialize mpg to 0.0, and later assign it when you know the values of liters and distance.

Post your new code to get help with the new error.

>> you want to declare the variables at the top of the function, and preform the calculation[s] on them later on

This is not true for C++. Declare your variables only when and where you need them, and initialize them fully when they are declared.
• 09-27-2005
dwks
You can do this, you know:
Code:

```int main() {     int x, y = 3;     x = y * y; }```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last