I wrote my program. It compiles, but when I run it, it doesn't print out my data, just my headers. Well, it prints my first header once, and then my second headers four times. Any thoughts and help would be greatly appreciated.
Code:
#include<stdio.h>
int main()
{
int carNum, carMiles, carGal;
float galsMile;
int totalMiles, totalAve, totalGal;
FILE *in;
totalGal = 0;
totalMiles = 0;
in = fopen("cardata.txt", "r");
if (in == NULL)
{
printf("Could not open file. \n");
exit(1);
}
printf("Car Number Miles Gallons Miles Per Gallon \n");
while((fscanf(in,"%d %d %d", &carNum, &carMiles, &carGal)) != EOF)
{
galsMile = carMiles / carGal;
fprintf(in, "%d %d %d %.2f \n", carNum, carMiles, carGal, galsMile);
totalGal = totalGal + carGal;
totalMiles = totalMiles + carMiles;
totalAve = totalMiles / totalGal;
printf("Total Gallons Total Miles Total Mile Per Gallon \n");
fprintf(in, "%d %d %.2f \n", totalGal, totalMiles, totalAve);
}
fclose(in);
return 0;
}