Question: #include #include #include /******************************************************************************* * List preprocessing directives - you may define your own. *******************************************************************************/ #define MAX_FLIGHTCODE_LEN 6 #define MAX_CITYCODE_LEN 3 #define MAX_NUM_FLIGHTS 5 #define
#include
#include
#include
/******************************************************************************* * List preprocessing directives - you may define your own. *******************************************************************************/ #define MAX_FLIGHTCODE_LEN 6 #define MAX_CITYCODE_LEN 3 #define MAX_NUM_FLIGHTS 5 #define DB_NAME "database.txt"
/******************************************************************************* * List structs - you may define struct date and struct student only. Each * struct definition should have only the fields mentioned in the assignment * description. *******************************************************************************/
struct date_time { int month; int day; int hour; int minute; }; typedef struct date_time date_time_t;
struct flight { char flightcode [MAX_FLIGHTCODE_LEN+1]; date_time_t departure_t; char arrival_city [MAX_CITYCODE_LEN+1]; date_time_t arrival_t; }; typedef struct flight flight_t;
int size = 0;
/******************************************************************************* * Function prototypes - do NOT change the given prototypes. However you may * define your own functions if required. *******************************************************************************/ void print_menu (void); void make_flight(int counter, flight_t f[]); int check_flight(int counter, flight_t flights[], char f[MAX_FLIGHTCODE_LEN+1]); void print_flight(int counter, flight_t flights[], char f[MAX_FLIGHTCODE_LEN+1]); void print_flights_all(int counter, flight_t flights[counter]); int write_flight(flight_t flights[], int counter); int read_flight(flight_t flights[], int counter);
/******************************************************************************* * Main *******************************************************************************/ int main(void) { int choice; char second_choice[MAX_FLIGHTCODE_LEN+1]; int counter=0; int test; flight_t flights[MAX_NUM_FLIGHTS];
do { fflush(stdin); print_menu(); scanf("%d", &choice); int choice; if ((1 == scanf("%d", &choice)) && choice >= 1 && choice <= 5) {
switch(choice) { case 1: { make_flight(counter, flights); counter++; break; } case 2: { printf("Enter arrival city code or enter * to show all destinations> "); scanf("%s",second_choice);
if (second_choice[0]=='*') { print_flights_all(counter, flights); } else if (counter<=0) { printf("No flights "); } else { test=check_flight(counter, flights, second_choice); if (test==0) { printf("No flights "); } else { print_flight(counter, flights, second_choice); } } break; } case 3: { write_flight(flights, counter); break;
} case 4: { int read_counter; FILE* fpr = NULL; fpr = fopen(DB_NAME, "r"); if (fpr == NULL) { printf("Read error "); break; } fscanf(fpr, "%d", &counter); for (read_counter=0; read_counter /******************************************************************************* * Functions *******************************************************************************/ /* Displays menu */ void print_menu (void) { printf(" " "1. add a flight " "2. display all flights to a destination " "3. save the flights to the database file " "4. load the flights from the database file " "5. exit the program " "Enter choice (number between 1-5)> "); } /* Prompts user to input flight details */ void make_flight(int counter, flight_t flights[]) { int s; int i; s = 1; printf("Enter flight code> "); scanf("%s", flights[counter].flightcode); while(s == 1){ s = 0; if(strlen(flights[size].flightcode) < 3 || strlen(flights[size].flightcode) >6 ){ printf("Invalid Input "); s= 1; } else if((flights[size].flightcode[0]<'A') || (flights[size].flightcode[0]>'Z')) { printf("Invalid Input "); s=1; } else if((flights[size].flightcode[1]<'A') || (flights[size].flightcode[1]>'Z')) { printf("Invalid Input "); s=1; } if(s==0){ for(i = 2; i < strlen(flights[size].flightcode); i++){ if((flights[size].flightcode[i]<'0') || (flights[size].flightcode[i]>'9')) { printf("Invalid Input "); s = 1; break; } } } if(s==1){ printf("Enter flight code> "); scanf("%6s", flights[size].flightcode); } if(s==0) break; } printf("Enter departure info for the flight leaving SYD. "); do { printf("Enter month, date, hour and minute separated by spaces> "); scanf("%d %d %d %d", &flights[counter].departure_t.month, &flights[counter].departure_t.day, &flights[counter].departure_t.hour, &flights[counter].departure_t.minute); if (12>flights[counter].departure_t.month|| flights[counter].departure_t.month>=0 || 31 /* Checks for existance of a flight in the program */ int check_flight(int counter, flight_t flights[], char second_choice[MAX_FLIGHTCODE_LEN+1]) { int second_counter; int incrementer=0; for (second_counter=0; second_counter<1; second_counter++) { if (strncmp(second_choice, flights[second_counter].arrival_city,4)==0) { incrementer++; } } return incrementer; } 1- I am only allow to use 2-For the add flight in Enter date and time it crash too (wrong number or letter) so if the user enter a wrong choice I want a messge with "Invalid input" shows up for both time and date - departure and arrival Can you fix it pls?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
