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:
--------------------------------------------------------------------------------------
#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;
}
--------------------------------------------------------------------------------------
#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[]);
--------------------------------------------------------------------------------------
/*function searches schedule for times closest to input 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 departure time with current closest time*/
if (closest < 0)
{
closest *= -1; /*set 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;
}
If someone could please tell me what I could be doing wrong, I would greatly appreciate it--thanks!
RICH