Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/* I want to fix void make_flight(int counter, flight_t flights[]) Enter flight

ID: 3742313 • Letter: #

Question

/*

I want to fix void make_flight(int counter, flight_t flights[])

Enter flight code>

Enter departure info for the flight leaving SYD.

Enter month, date, hour and minute separated by spaces>

Enter arrival city code>

Enter arrival info.

Enter month, date, hour and minute separated by spaces>

It should do all of this:

1-Flight - left aligned, MAX_FLIGHTCODE_LEN (i.e. 6) chars at most.

2-City - left aligned, MAX_CITYCODE_LEN 3 chars at most . For example( VA1 or LAX )

3- Month, day, hour, minute - two digits with leading zeros

Your program should be able to check the format of the flightcode.

The first two characters of the flightcode must be uppercase letters (A-Z)

representing the airline. The rest of the flightcode should be numerals (0-9)

representing the flight number.

There must be 1-4 numerals as the flight number part of the flightcode.

No spaces in the flightcode.

*/

Month, day, hour, minute - two digits with leading zeros

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_FLIGHTCODE_LEN 6

#define MAX_CITYCODE_LEN 3

#define MAX_NUM_FLIGHTS 5

#define DB_NAME "database"

struct date_time

{

int minute;

int hour;

int day;

int month;

};

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;

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);

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<counter; read_counter++)

{

fprintf(fpr, "%-6.6s SYD %02d-%02d %02d:%02d %-3.3s %02d-%02d %02d:%02d ", flights[read_counter].flightcode, flights[read_counter].departure_t.month, flights[read_counter].departure_t.day, flights[read_counter].departure_t.hour, flights[read_counter].departure_t.minute, flights[read_counter].arrival_city, flights[read_counter].arrival_t.month, flights[read_counter].arrival_t.day, flights[read_counter].arrival_t.hour, flights[read_counter].arrival_t.minute);

}

fclose(fpr);

break;

}

case 5:

{

exit(1);

break;

}

default:

{

printf("Invalid choice ");

break;

}

}

}

while(choice!=5);

return 0;

}

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)> ");

}

void make_flight(int counter, flight_t flights[])

{

printf("Enter flight code> ");

scanf("%s", flights[counter].flightcode);

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<flights[counter].departure_t.day || flights[counter].departure_t.day<=0 || 24<=flights[counter].departure_t.hour || flights[counter].departure_t.hour<0 || 60<=flights[counter].departure_t.minute || flights[counter].departure_t.minute<0)

{

printf("Invalid input ");

}

} while (12<flights[counter].departure_t.month|| flights[counter].departure_t.month<=0 || 31<flights[counter].departure_t.day || flights[counter].departure_t.day<=0 || 24<=flights[counter].departure_t.hour || flights[counter].departure_t.hour<0 || 60<=flights[counter].departure_t.minute || flights[counter].departure_t.minute<0);

printf("Enter arrival city code> ");

scanf("%s", flights[counter].arrival_city);

printf("Enter arrival info. ");

do

{

printf("Enter month, date, hour and minute separated by spaces> ");

scanf("%d %d %d %d", &flights[counter].arrival_t.month, &flights[counter].arrival_t.day, &flights[counter].arrival_t.hour, &flights[counter].arrival_t.minute);

if (12<flights[counter].arrival_t.month|| flights[counter].arrival_t.month<=0 || 31<flights[counter].arrival_t.day || flights[counter].arrival_t.day<=0 || 24<=flights[counter].arrival_t.hour || flights[counter].arrival_t.hour<0 || 60<=flights[counter].arrival_t.minute || flights[counter].arrival_t.minute<0)

{

printf("Invalid input ");

}

} while (12<flights[counter].arrival_t.month|| flights[counter].arrival_t.month<=0 || 31<flights[counter].arrival_t.day || flights[counter].arrival_t.day<=0 || 24<=flights[counter].arrival_t.hour || flights[counter].arrival_t.hour<0 || 60<=flights[counter].arrival_t.minute || flights[counter].arrival_t.minute<0);

}

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;

}

Explanation / Answer

/*

I've added two new functions for validation

insert in your code as shown

I've fixed the validations, this should work

also make your problem more clear weather the whole program is to be solved

*/

int check_filghtcode(char *fc) {
int ret = fc[0] >= 'A' && fc[0] <= 'Z' &&
fc[1] >= 'A' && fc[1] <= 'Z' &&
fc[2] >= '0' && fc[2] <= '9', i;
for(i = 3; fc[i] != ''; i++) {
if(!(fc[i] >= '0' && fc[i] <= '9'))
return 0;
}
return ret && i < 7;
//checks if the string has ended
//returns 1 is success, 0 if failed
}

int check_date(int month, int date, int hour, int minute) {
int days[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
return month >= 0 && month < 12 &&
date >= 0 && date < days[month] &&
hour >= 0 && hour < 23 &&
minute >= 0 && minute < 59;
}

void make_flight(int counter, flight_t flights[]) {
do {
printf("Enter flight code> ");
scanf("%s", flights[counter].flightcode);
if(check_filghtcode(flights[counter].flightcode) != 1)
printf("INvalid input ");
} while(check_filghtcode(flights[counter].flightcode) != 1);

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 (check_date(
flights[counter].departure_t.month,
flights[counter].departure_t.day,
flights[counter].departure_t.hour,
flights[counter].departure_t.minute
) != 1) {
printf("Invalid input ");
}
} while (check_date(
flights[counter].departure_t.month,
flights[counter].departure_t.day,
flights[counter].departure_t.hour,
flights[counter].departure_t.minute
) != 1);

printf("Enter arrival city code> ");
scanf("%s", flights[counter].arrival_city);
printf("Enter arrival info. ");
do {
printf("Enter month, date, hour and minute separated by spaces> ");
scanf("%d %d %d %d", & flights[counter].arrival_t.month, & flights[counter].arrival_t.day, & flights[counter].arrival_t.hour, & flights[counter].arrival_t.minute);
if (check_date(
flights[counter].arrival_t.month,
flights[counter].arrival_t.day,
flights[counter].arrival_t.hour,
flights[counter].arrival_t.minute
) != 1){
printf("Invalid input ");
}
} while (check_date(
flights[counter].arrival_t.month,
flights[counter].arrival_t.day,
flights[counter].arrival_t.hour,
flights[counter].arrival_t.minute
) != 1);
}