Help with looping

Hey guys, about 3 weeks into my first C++ class and I'm writing a program to calculate mpg. I have the code pretty much worked out, I have to keep a running
total of the gallons used, miles driven and total mpg. I have the total mpg and miles driven calculating right, but I'm having trouble with the total gallons. I have to run the program three times with different variables, the total gallons is now calculating the input from outside the loop. Any advice?
[code]


int main(void) {

StartUp ();
float milesdriven; // total miles driven
float endmiles; // ending mileage
float startmiles; // starting mileage
float gallons; // read in the gallons
float totalgallons; // total gallons used
float totalmiles; // total miles driven
float prevmiles; // previous starting mileage
float milespergallon; // Mpg average
float endingmiles;
float milesgallon;
float totalmilespergallon;

cout << "Enter the gallons used (-1 to end): ";
cin >> gallons;
cout << setprecision(2);
cout << fixed;

cout << '\n';
cout << "Enter the starting mileage: ";
cin >> startmiles;

if (gallons != -1)
{
cout << "Enter the ending mileage: ";
cin >> endingmiles;
milesdriven = endingmiles - startmiles;
cout << "Miles Driven: " << milesdriven << endl;
milespergallon = milesdriven / gallons;
cout << "The miles / gallon for this tank was " <<
setprecision(6) << milespergallon << endl;
cout << '\n';
cout << "Total gallons used: " << setprecision(1) << gallons
<< endl;
cout << "Total miles driven: " << setprecision(0) <<
milesdriven << endl;
cout << "The overall average miles / gallon was: " <<
setprecision(6) << milespergallon << endl;
cout << '\n';


while (gallons != -1)
{
totalgallons =+ gallons;
cout << "Enter the gallons used (-1 to end): ";
cin >> gallons;
if (gallons != -1)
{
cout << "Enter the starting mileage: ";
cin >> startmiles;
cout << "Enter the ending mileage: ";
cin >> endmiles;
prevmiles = endmiles - endingmiles;
cout << "Miles driven (ending - starting):" <<
setprecision(1) << prevmiles << endl;
;milesgallon = prevmiles / gallons;
cout << "The miles / gallon for this tank was " <<
setprecision(6) << milesgallon << endl;
cout << '\n';
cout << '\n';





totalmiles = endmiles;
}
totalgallons += gallons;
cout << "Total gallons used: " << setprecision(2) << totalgallons << endl;
cout << "Total miles driven: " << setprecision(2) <<totalmiles << endl;
totalmilespergallon = milesgallon + milespergallon;
cout << "The overall average miles / gallon was: " <<
setprecision(6) << totalmilespergallon << endl;
cout << '\n';
cout << '\n';



}
}

WrapUp();
return Success;

} // End of function main
I'd start with Initialization of variables.
Topic archived. No new replies allowed.