'const int' vs. #define and ouput issues
Code:
/*******************************************************
* Purpose: Design an airline reservation structure that contains the following data:
*
* Flight Number
* Originating Airport Code (3 characters)
* Destination Airport Code (3 characters)
* Starting time
* Arrival time
***************************************************************/
#include <stdio.h>
struct itinerary
{
int flight; /* Flight number */
char origin_airport[3]; /* Airport of origin */
char dest_airport[3]; /* Destination airport */
int start_time; /* Time of start of flight */
int arrival_time; /* Time of arrival */
};
#define MAX_ENTRIES 50 /* Max number of stucts to create */
struct itinerary list[MAX_ENTRIES]; /* Array of structures for data */
void print_itinerary(int counter); /* Prototype for printing out info */
int main()
{
char line[10]; /* Input from the user */
int counter = 0; /* Counter for loop starts at zero */
char quit; /* Variable used to exit loop */
while(counter <= MAX_ENTRIES)
{
printf("Enter the flight number: ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%d", &list[counter].flight);
printf("Enter the airport origin: ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%s", &list[counter].origin_airport);
printf("Enter the destination airport: ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%s", &list[counter].dest_airport);
printf("Enter the start time: ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%d", &list[counter].start_time);
printf("Enter the arrival time: ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%d", &list[counter].arrival_time);
printf("Do you wish to enter another flight? (<enter>=yes or n=no) ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%c", &quit);
if(quit == 'n')
break;
++counter;
}
print_itinerary(counter);
return(0);
}
void print_itinerary(int counter)
{
int index = 0; /* Index to cycle through the program */
while(index <= counter)
{
printf("Flight: %d \n", list[index].flight);
printf("Origin Airport: %s \n", list[index].origin_airport);
printf("Desination Airport %s \n", list[index].dest_airport);
printf("Start: %d \n", list[index].start_time);
printf("Arrive: %d \n", list[index].arrival_time);
printf("\n");
++index;
}
return;
}
Two questions:
1) Why do I have to use '#define' instead of 'const int' I get an error message with 'const int':
error: variable-size type declared outside of any function 'const int'
$gcc --version
gcc (GCC) 3.4.2 [FreeBSD] 20040728
2) The print to screen output I get is:
Enter the flight number: 110
Enter the airport origin: TLH
Enter the destination airport: SEA
Enter the start time: 1110
Enter the arrival time: 1230
Do you wish to enter another flight? (<enter>=yes or n=no) n
Flight: 110
Origin Airport: TLHSEA
Desination Airport SEA
Start: 1110
Arrive: 1230
As you can see I get TLH and SEA put together in the ouput to screen. This may be obvious to you guys.
Thanks in advance for any and all help.