Is the statement I'm using simply not something you can do with a struct?
No, you can. I would have told you if it was something you couldn't do.
Also I'm not really sure what to tell you. It works for me.
Code:
#include <stdio.h>
#define MAX_CHARS 64
struct car{
char make[MAX_CHARS + 1];
char model[MAX_CHARS + 1];
int year;
int city_mpg;
int highway_mpg;
double avg_mpg;
};
int main(void)
{
/* Make(string) Model(string) Year(int) Highway MPG(int) City MPG(int) */
const char *fakefile[3] =
{
"Honda Accord 2016 23 34",
"Mazda CX-5 2016 26 35",
"Jeep.Grand.Cherokee Laredo 2016 18 25"
};
struct car cars[3];
int i = 0;
while (i < 3 && sscanf(fakefile[i], "%s %s %d %d %d", cars[i].make, cars[i].model,
&cars[i].year, &cars[i].city_mpg, &cars[i].highway_mpg) == 5)
{
cars[i].avg_mpg = (cars[i].city_mpg + cars[i].highway_mpg) / 2.0;
printf("%s %s average mpg=%f\n", cars[i].make, cars[i].model, cars[i].avg_mpg);
++i;
}
return 0;
}
Honda Accord average mpg=28.500000
Mazda CX-5 average mpg=30.500000
Jeep.Grand.Cherokee Laredo average mpg=21.500000
Maybe there is something you missed?