# Small program that has to calculate miles per gallon

• 01-05-2004
Guti14
Small program that has to calculate miles per gallon
Hi,

I am developing a program that should calculate the miles per gallon obtained from tankful. After processing all the input data, the program should calculate the combined miles per gallon obtained for all tankfuls.

So, this is what the output should look like -

Enter the gallons user( -1 to end): 12.8
Enter the miles driven: 287
The miles / gallon for this tank was 22.421875

Enter the gallons user( -1 to end): 10.3
Enter the miles driven: 200
The miles / gallon for this tank was 19.417475

Enter the gallons user( -1 to end): -1

The overall average miles/gallon was 21.601423

This is the code Iv'e done so far. It doesn't give the desired result, as it, does like an endless loop after Iv'e entered the gallons and miles.
Code:

```#pragma argsused #pragma hdrstop #include<iomanip> #include<iostream> #include<conio> using namespace std; int main(int argc, char* argv[]) {   // declare the variables   double gallons;      // read in the gallons   double miles;        // read in the miles   double totalGallons; // calculate the total gallon input   double totalMiles;  // calculate the total mile input   double total;        // calculate the gallons/miles   double average;      // calculate the average overall   double counter;   // initialization phase   total = 0;      // initialise total   counter = 1;    // initialise loop counter   cout << "Enter the gallons used( -1 to end): " << endl;   cin >> gallons;   cout << "Enter the miles driven: " << endl;   cin >> miles;   average = miles / gallons; // calculate the the current input   cout << average;  // display average   // loop until sentinal value read from user   while ( gallons != -1 ) {       total = totalMiles + totalGallons;//add total of gallons to total of miles       counter = counter + 1;  // increment the counter       // prompt for input and read the next gallons       cout << "Enter the gallons user( -1 to end): " << endl;       cout << "Enter the miles driven: " << endl;   }   // termination phase   // if user entered at least one gallon...   if ( counter != 0 ) {       // calculate average of all gallons over miles entered       average = static_cast< double >( total ) / counter;       // display overall average with six digits of precision       cout << "Overall average is " << setprecision( 6 )           << fixed << average << endl;       }       else         cout << "No gallons or miles were entered" << endl;   getch();   return 0; }```
• 01-05-2004
Lurker
Code:

```  while ( gallons != -1 ) {       total = totalMiles + totalGallons;//add total of gallons to total of miles       counter = counter + 1;  // increment the counter       // prompt for input and read the next gallons       cout << "Enter the gallons user( -1 to end): " << endl;       cout << "Enter the miles driven: " << endl;   }```
How do you expect the user to enter the gallons and miles if it never asks for a value?
• 01-05-2004
WaltP
Code:

`total = totalMiles + totalGallons;//add total of gallons to total of miles`
And of what value is total? The combination of gallons and miles gives you a worthless number. And anyway, you never set totalMiles nor totalGallons any value.
• 01-05-2004
Aalmaron
i looked through it, and changed it a little, and this is what i got.

Code:

```#pragma argsused #pragma hdrstop #include<iomanip> #include<iostream> #include<conio.h> using namespace std; int main(int argc, char* argv[]) {   // declare the variables   double gallons;      // read in the gallons   double miles;        // read in the miles   double gallons2;   double miles2;   double average3;   double average4;   double average;      // calculate the average overall   double average2;   cout << "Enter the gallons used( 0 to end): " << endl;   cin >> gallons;   cout << "Enter the miles driven: " << endl;   cin >> miles;   average = miles / gallons; // calculate the the current input   cout << "your average for the trip was " << average << endl;  // display average   // loop until sentinal value read from user   if ( gallons != 0 )   {       // prompt for input and read the next gallons       cout << "Enter the gallons used( 0 to end): " << endl;       cin >> gallons2;       cout << "Enter the miles driven: " << endl;       cin >> miles2;       average2 = miles2 / gallons2;       cout << "your average for trip 2 was " << average2 << endl;   }         else       {         cout << "No gallons or miles were entered" << endl;   return 0;     }   // termination phase   // if user entered at least one gallon...   if ( gallons2 != 0 )   {       average3 = average + average2;  // to get the average of the  2       average4 = average3 / 2;  // to get the average of the 2       // display overall average with six digits of precision       cout << "Overall average is " << average4;       cin.get();     }       else     {         cout << "No gallons or miles were entered" << endl;   return 0;     } }```
sorry for my lack of comenting. but you should be able to figure it out.

it worked for me, but it didnt stay up to give me the over all average. maybe one of you can see what was wrong.

(note: sorry if its redundant/ not clean, still a noob)
• 01-06-2004
WaltP

It would help if you fix your format so the indentation is consistant.

And if you're going to apologize for not commenting, comment and save the apology ;)
• 01-06-2004
WDT
Plain and simply- as pointed out by Lurk & Aalmaron- you forgot to read in the values for the variables in your loop i.e. cin>>gallons; and the other. Always make sure you can reach the exit conditon you set for a loop.
• 01-06-2004
Aalmaron
oh hey, if you wanted to loop it, among other things that you'd need to change, you shouldnt have the paramiters be the gas.

Code:

```int counter = 1 while ( counter = 1) {  yadda yadda yadda  counter = counter + 1; } while ( counter = 2) {  yadda yadda yadda  counter = counter + 1; }```
then at the end of the loop add

Code:

`counter = 1`
you could incorperate that into the code and also mess with the way the math works.