Good evening! I am trying to build a C program for a very small flight database.
ID: 674036 • Letter: G
Question
Good evening!
I am trying to build a C program for a very small flight database.
Here is an overview of what I am trying to do.
1) Read and store data from three files (airports.txt routes.txt flights.txt) into arrays.
2) Make an interface (main), allowing the user to enter their choices.
3) Based on the data and what the user wants, print out various flight information and airport information
Here is an example of the flights.txt file
The format: <Flight Number><Route ID><Departure Time><Arrival Time><Days Run>
Here are the assumptions:
•Flight Numbers may be skipped, so don’t just go in order
•Departure and Arrival Times of the 12 hour format: hour:minutea for AM or hour:minutep for PM
•Days Run is either “Daily” which is all days. It may be a list of numbers like “123” meaning the days it runs (with 1 being Sunday, 2 being Monday and so on). It may also have an X in front of it, meaning the days it does not run. May want to convert this into a friendlier format.
•Max of 1000 flights
Here is how to go about reading in the data:
•Similar to routes.txt
•Get a line either using fgets(line, MAX_LINE_LENGTH, fp) or going through the line, reading in chars and storing them in the line string
•sscanf on the line for all the fields
•When storing the fields, may be good idea to convert the times into military time. You will need to compare them and it’s easier to compare.
Please store them in these global areas I have set:
Explanation / Answer
#include<stdio.h>
#include<string.h>
int main()
{
FILE* fp;
char line[255];
int MAX_FLIGHTS = 10;
int MAX_ROUTES = 5;
int MAX_AIRPORTS = 3;
int i=0; //counter iterator while filling arrays
//declaring arrays
char flight_num[MAX_FLIGHTS];
char route_id[MAX_FLIGHTS];
char Dep_time[MAX_FLIGHTS];
char Arr_time[MAX_FLIGHTS];
char days_run[MAX_FLIGHTS];
char route_Num[MAX_ROUTES];
char airport[MAX_AIRPORTS];
char airport_cities[MAX_AIRPORTS];
fp = fopen("flights.txt" , "r");
while( fgets(line, sizeof(line), fp) != NULL ){
char first[16] ,second[16],third[16] ,fourth[16],five[16];
char *pos;
if ((pos=strchr(line, ' ')) != NULL)
*pos = '';
//reading each line data from text file
strcpy(val1 , strtok(line," "));
strcpy(val2 , strtok(line," "));
strcpy(val3 , strtok(line," "));
strcpy(val4 , strtok(line," "));
strcpy(val5 , strtok(NULL," "));
//storing back data to arrays
flight_num[i] = val1;
route_id[i] = val2;
Dep_time[i] = val3;
Arr_time[i] = val4;
days_run[i] = val5;
i++;
}
i=0;
fp = fopen("routes.txt" , "r");
while( fgets(line, sizeof(line), fp) != NULL ){
char *pos;
if ((pos=strchr(line, ' ')) != NULL)
*pos = '';
//reading each line data from text file
route_Num[i] = line;
i++;
}
i=0;
fp = fopen("airport.txt" , "r");
while( fgets(line, sizeof(line), fp) != NULL ){
char first[16] ,second[16];
char *pos;
if ((pos=strchr(line, ' ')) != NULL)
*pos = '';
//reading each line data from text file
strcpy(first , strtok(line,","));
strcpy(second , strtok(NULL,","));
airport[i] = first;
airport_cities[i] = second;
i++;
}
//printing menu
printf(" Enter 1.flights data | 2.Routes data | 3.Airport data");
int choice;
scanf("%d",&choice);
//printing menu
if(choice == 1){
for(i=0;i<MAX_FLIGHTS;i++){
printf(" %s |%s | %s | %s | %s",flight_num[i], route_id[i],Dep_time[i],Arr_time[i],days_run[i]);
}
}
else if(choice == 2){
for(i=0;i<MAX_ROUTES;i++){
printf(" %s",route_Num[i]);
}
}
else{
for(i=0;i<MAX_AIRPORTS;i++){
printf(" %s | %s",airport[i],airport_cities[i]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.