title: Read from file and do calculations. C program
body: <p>I need to write C program that reads from file the car number, miles driven, and gallons used. Calculate the miles per gallon. Calculate the totals and the average MPG. </p>
  
  <p>I only need help with counting miles per gallon. </p>
  
  <pre><code>In output should be :20    But my output is: 1966018914
                       25                      20
                       24                      25
                       23                      24
  </code></pre>
  
  <p>Can anyone see my code and help me figure it out?!</p>
  
  <p>Here is code:    </p>
  
  <pre><code>int main()
  {
      int car, miles, gas;
      int sumMiles = 0;
      int sumGas = 0;
      int avgMPG = 0;
      FILE *inFile, *outFile;
      char fname[20];
  
      printf("Enter a file name: ");
      gets(fname);
  
      inFile = fopen(fname, "r");
      if (inFile == NULL)
      {
          printf("\nFailed to open file.\n");
          exit(1);
      }
  
      outFile =  fopen("output.txt","w");
  
      if(outFile==NULL)
      {
          printf("The file was not opened.");
          exit(1);
      }
  
      printf("\nCar No.   Miles Driven    Gallons Used\n");
  
      while (fscanf(inFile, "%d %d %d",&amp;car, &amp;miles, &amp;gas) != EOF)
      {
          printf("%-7d %-15d %d\n",car,miles,gas);
          sumMiles += miles;
          sumGas += gas;
          avgMPG = sumMiles / sumGas;
      }
  
      printf("\nThe total miles driven is %d\n", sumMiles);
      printf("The total gallons of gas used is %d\n", sumGas);
      printf("The average miles per gallon of gas used is %d\n", avgMPG);
  
      printf("File copied succesfully!");
      fclose(inFile);
      fclose(outFile);
  
  }
  </code></pre>
  
  <p>This is input file:</p>
  
  <pre><code>123 100 5
  345 150 6
  678 240 10
  901 350 15
  </code></pre>