I'm pretty lost when it comes to reading/writing, I've paid attention in my class, read multiple online tutorials, and the book and I can't figure it out. I tried to do this assignment, but it just crashes when I execute it.
Code:#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAXNUM 6
int main()
{
FILE *fp;
char *mode = "r";
int i=0;
char fName[MAXNUM] = {0}, lName[MAXNUM] = {0};
int carNum[MAXNUM] = {0}, milesDriven[MAXNUM] = {0}, usedGallons[MAXNUM] = {0};
double milesPerGallon[MAXNUM] = {0};
int totalMiles = 0, totalGallons = 0;
double totalMPG = 0, averageMPG = 0;
fp = fopen("cars.dat", "r");
if (fp == NULL)
{
printf("Can't open input file cars.dat!\n");
exit(1);
}
for (i=0; i<MAXNUM; i++)
{
fscanf(fp, "%c %c %d %d %d", fName[i], lName[i], carNum[i], milesDriven[i], usedGallons[i]); //Assigns variables from file
totalMiles += milesDriven[i]; //Sums total miles
totalGallons += usedGallons[i]; //Sums total gallons
milesPerGallon[i] = (milesDriven[i] / usedGallons[i]); //Calculates MPG
}
for (i=0; i<MAXNUM; i++)
totalMPG += milesPerGallon[i]; //Sums total MPG
averageMPG = (totalMPG / MAXNUM); //Calculates Avg MPG
printf("Last_Name First_Name Car_No. Miles_Driven Gallons_Used MPG"); //print header
for (i=0; i<MAXNUM; i++) //prints charts
printf("%c %c %d %d %d %d\n", fName[i], lName[i], carNum[i], milesDriven[i], usedGallons[i], milesPerGallon[i]);
fclose(fp);
getch();
return 0;
}

