Hi everyone,
It seems my struggles with structures have continued. In my current program, I'm trying to pass an array of structures into a function. I've declared my structure in a separate header file using typedef to define a new type called 'flight'. But when I try to compile my program, I get the following error message:
"Syntax error before flight"
Here is my code for main(), my header file "defs.h", and my function "find_depart()", respectively:
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------Code:#include "defs.h" int main(int argc, char *argv[]) { int time, closest; puts("Enter a time in military hours and minutes:"); scanf("%d", &time); flight schedule[] = /*flight is a structure type*/ {{800, 1016, "Jason Mackenzie"}, {943, 1152, "Valerie Woods"}, {1119, 1331, "Antonio Vasquez"}, {1247, 1500, "Natalie McIver"}, {1400, 1608, "Scott Curtis"}, {1545, 1755, "Yvonne Vogelar"}, {1900, 2120, "Mitch Matthews"}, {2145, 2358, "Marcie Maddox"}}; find_departure(time, flight schedule[]); return 0; }
------------------------------------------------------------------------------------------------------Code:#include <stdio.h> #include <string.h> #define FLIGHTS 8 typedef struct { int depart; int arrive; const char *attendant; } flight; /*flight is now a new structure type*/ int find_departure(int time, flight schedule[]);
If someone could please tell me what I could be doing wrong, I would greatly appreciate it--thanks!Code:/*function to search flight schedule for times closest to time put in by user*/ #include "defs.h" int find_departure(int time, flight schedule[]) { int j = 0; /*counter to search schedule*/ int closest = time - schedule.depart[0]; /*initially, the closest flight time is the first departure*/ int temp; /*used to compare each departure time with current closest flight time*/ if (closest < 0) { closest *= -1; /*set closest as absolute value if negative*/ } while (j <= FLIGHTS) { temp = time - schedule.depart[j]; if (temp < 0) { temp *= -1; /*temp is absolute value if negative*/ } if (temp < closest) { closest = schedule.depart[j]; /*current departure is now closest to user's input*/ } j++; /*check next departure time*/ } return closest; }
RICH



LinkBack URL
About LinkBacks


