Program help

What's wrong with my program? It will compile and run but doesn't do the calculations. It also says my variables are being used without being initialized. Please help. Thanks!

#include<iostream>

using namespace std;

int main ()
{
double stMileage;
double endMileage;
double stFuel;
double endFuel;

double miTrvld = endMileage - stMileage;
double fuelUsed = stFuel - endFuel;
double MPG = miTrvld / fuelUsed;

const double kmTrvld = miTrvld * 1.609;
const double fuelUsedL = fuelUsed * 3.785;
const double litersHundredKm = fuelUsedL / (kmTrvld * 100);

cout << "Enter starting mileage: ";
cin >> stMileage;
cout << endl;

cout << "Enter ending mileage: ";
cin >> endMileage;
cout << endl;

cout << "Enter fuel at start in gallons: ";
cin >> stFuel;
cout << endl;

cout << "Enter fuel at end in gallons: ";
cin >> endFuel;
cout << endl;

cout << "Number of miles traveled: "
<< miTrvld << " miles"
<< endl;

cout << "Fuel used: "
<< fuelUsed << " gallons"
<< endl;

cout << "Miles per gallon of fuel: "
<< MPG << endl;

cout << endl;

cout << "Number of kms traveled: "
<< kmTrvld << " kms"
<< endl;

cout << "Fuel used: "
<< fuelUsedL << " liters"
<< endl;

cout << "Liters per 100 kms: "
<< litersHundredKm
<< endl << endl;

system("pause");

return 0;

}
You use these variables before they have been initialized
1
2
3
4
double stMileage;
double endMileage;
double stFuel;
double endFuel;
I'm sorry, I'm not really sure how to fix that.
I wrote another program with a similar format and it worked fine.
Is the positioning of the variable declarations wrong?
No, it's mostly that you use them before they've been assigned any meaningful value.

1
2
double miTrvld = endMileage - stMileage;
double fuelUsed = stFuel - endFuel;


endMileage, stMileaage, stFuel and endFuel have random junk values. So miTrvld and fuelUsed is also going to be some random junk value.

And, you know what they say: Garbage in, garbage out.

The solution is to perform the calculations after getting the user input.
Last edited on
That makes sense to me, but my teacher keeps telling us to put all of our declarations at the beginning of the function.

So, should I declare them at the top, then put their calculations after the user input?
If only to satisfy your instructor.

Best practice is to declare/define variables as close as possible to first use.
It's a simple mistake that i've made many times (and occasionally still do)...

for example: you're trying to declare "miTrvld" as "stMilage - endMileage" but these two variables are still set as 0.
So what's happening is "miTrvld = 0 * 0"

FIX= Have all your questions BEFORE your calculations.
Then you'll have the actual values to be able to do the calculations with!


=======================================================

EDIT: you can still declare your variables first if you like and then assign them later but if you do this DO NOT declare you final values as constants...

example:

double miTrvld;
double stMileage;
double endMileage;

//then questions

miTrvld = endMileage - stMileage;
Last edited on
Got it. Thank you. :)
Topic archived. No new replies allowed.