Alright, changed it to 4, and I still get no errors but program crashes when I execute. I got to be doing something wrong with the file or assigning the string values. Heres my current code at the moment.
Code:
int main()
{
FILE *fp;
char *mode = "r";
int i=0;
char *fName[MAXNUM], *lName[MAXNUM];
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, "%s", &fName[i]); /**/
fscanf(fp, "%s", &lName[i]); /**/
fscanf(fp, "%d", &carNum[i]); /*Assigns variables from file*/
fscanf(fp, "%d", &milesDriven[i]); /**/
fscanf(fp, "%d", &usedGallons[i]); /**/
totalMiles += milesDriven[i]; /*Sums total miles*/
totalGallons += usedGallons[i]; /*Sums total gallons*/
milesPerGallon[i] = ((double)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\n"); /*print header*/
for (i=0; i<MAXNUM; i++)
{
printf("%c", fName[i]);
printf("%c", lName[i]);
printf("%d", carNum[i]);
printf("%d", milesDriven[i]);
printf("%d", usedGallons[i]);
printf("%d", milesPerGallon[i]);
printf("\n");
}
fclose(fp);
getch();
return 0;
}
The output it is giving me is weird, and the printf's seem right, so im assuming its how im assigning variables from the file. heres the output.
Code:
Last_Name First_Name Car_No. Miles_Driven Gallons_Used MPG
JA9723815-1145324612
M 67350100
AS9910080
JJ8812386-1997659207
TM561000800
Ms0000