title: Output for overall MPG is fractionally off (Visual C++)
body: <p>I created a program where you input the miles driven and the gallons used for each tankful, and the program displays the mpg for each tankful.  I am using Visual Studio 2010.  When I enter the sentinel value of -1, I am given the overall mpg.  Here is my code:</p>
  
  <pre><code>/* Name
  Lab 3 - Page 105, 3.16
  September 23th, 2015
  Page 2 of 3 */
  
  #include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  
  // function main begins program execution
  int main( void )
  {   
      // initialization phase
      unsigned int counter = 0; // number of tanks used
      float gallons = 0; // gallons
      float miles = 0; // miles
      float milesPerGallon = 0; // MPG
      float allTankMPG = 0; // sum of all tanks (MPGs)
  
      float averageMPG = 0; // average MPG of all tankfuls
  
      // processing phase
      // get first tankful information
      printf( "%s", "Enter the gallons used (-1 to end): " ); // prompt for gallons used
      scanf( "%f", &amp;gallons ); // read gallons input from user
  
      // loop while sentinel value not yet read from user
      while ( gallons != -1 ) {
  
          printf( "%s", "Enter the miles driven: " ); // prompt for miles driven
          scanf( "%f", &amp;miles ); // read miles input from user
  
          milesPerGallon = miles / gallons; // miles per gallon for the tank
          printf("%s%.6f\n\n", "The miles/gallon for this tank was: ", milesPerGallon ); // display milesPerGallon
          counter = counter + 1; // increment counter
          allTankMPG = allTankMPG + milesPerGallon; // add the miles per gallon the sum total of every tank's mpg
  
          // get next tankful information
          printf( "%s", "Enter the gallons used (-1 to end): " ); // prompt for gallons used
          scanf( "%f", &amp;gallons ); // read input of the next gallon from user
      } // end while
  
      // termination phase
      // if user entered at least one set of information for one tankful
      if ( counter != 0 ) {
  
          // calculate average MPG of all tankfuls
          averageMPG = (float) allTankMPG / counter; // avoid truncation
  
          // display average with six digits of precision
          printf( "\n%s%.6f\n\n", "The overall average miles per gallon was ", averageMPG );
      } // end if
  
      system( "pause" );
      return 0;
  } // end function main``
  </code></pre>
  
  <p>My output, however looks like this:</p>
  
  <ul>
  <li>Enter the gallons used (-1 to end): 12.8</li>
  <li>Enter the miles driven: 287</li>
  <li><p>The miles/gallon for this tank was: 22.421875</p></li>
  <li><p>Enter the gallons used (-1 to end): 10.3</p></li>
  <li>Enter the miles driven: 200</li>
  <li><p>The miles/gallon for this tank was: 19.417475</p></li>
  <li><p>Enter the gallons used (-1 to end): 5</p></li>
  <li>Enter the miles driven: 120</li>
  <li><p>The miles/gallon for this tank was: 24.000000</p></li>
  <li><p>Enter the gallons used (-1 to end): -1</p></li>
  </ul>
  
  <p>The overall average miles per gallon was 21.946449</p>
  
  <p>Press any key to continue . . .</p>
  
  <p>Now according to the book, the overall average MPG should be 21.601423, not 21.946449 when the same exact input is entered.  Can anyone please elaborate on why my output is fractionally off?  Thank you, it is much appreciated.</p>