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

How to fix this issue, when the first menu appers the user cannot type anything

ID: 3742643 • Letter: H

Question

How to fix this issue, when the first menu appers the user cannot type anything except numbers between 1 to 5. So a user tryto type for example 8 or afhf or anything no action occurs and nothing appear on screen unless he type 1 or 2 or 3 or 4 or 5.

#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"


/*******************************************************************************
* 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;

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

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 {
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;
}

/*******************************************************************************
* 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[])
{
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 {
printf("Invalid input ");
}
} while (12 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 {
printf("Invalid input ");
}
} while (12 }

/* 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;
}

/* Displays all flight details inputted by the user */
void print_flights_all(int counter, flight_t flights[counter])
{
int second_counter;
printf("Flight Origin Destination ");
printf("------ --------------- --------------- ");
for (second_counter=0; second_counter {
printf("%-6.6s SYD %02d-%02d %02d:%02d %-3.3s %02d-%02d %02d:%02d ", flights[second_counter].flightcode, flights[second_counter].departure_t.month, flights[second_counter].departure_t.day, flights[second_counter].departure_t.hour, flights[second_counter].departure_t.minute, flights[second_counter].arrival_city, flights[second_counter].arrival_t.month, flights[second_counter].arrival_t.day, flights[second_counter].arrival_t.hour, flights[second_counter].arrival_t.minute);
}
}

/* Displays current flight details indicated by the flightcode */
void print_flight(int counter, flight_t flights[], char second_choice[MAX_FLIGHTCODE_LEN+1])
{
int second_counter;
printf("Flight Origin Destination ");
printf("------ --------------- --------------- ");
for (second_counter=0; second_counter<1; second_counter++)
{
if (strncmp(second_choice, flights[second_counter].arrival_city, 4)==0)
{
printf("%-6.6s SYD %02d-%02d %02d:%02d %-3.3s %02d-%02d %02d:%02d ", flights[second_counter].flightcode, flights[second_counter].departure_t.month, flights[second_counter].departure_t.day, flights[second_counter].departure_t.hour, flights[second_counter].departure_t.minute, flights[second_counter].arrival_city, flights[second_counter].arrival_t.month, flights[second_counter].arrival_t.day, flights[second_counter].arrival_t.hour, flights[second_counter].arrival_t.minute);
}
}

}

/* Saves flight details in seperate text file called 'database.txt */
int write_flight(flight_t flights[], int counter)
{
int write_counter;
FILE* fpw = NULL;
fpw = fopen(DB_NAME, "w");
if (fpw == NULL)
{
printf("Read error ");
return 1;
}
fprintf(fpw, "%d", counter);
for(write_counter=0; write_counter {
fprintf(fpw, "%-6.6s SYD %02d-%02d %02d:%02d %-3.3s %02d-%02d %02d:%02d ", flights[write_counter].flightcode, flights[write_counter].departure_t.month, flights[write_counter].departure_t.day, flights[write_counter].departure_t.hour, flights[write_counter].departure_t.minute, flights[write_counter].arrival_city, flights[write_counter].arrival_t.month, flights[write_counter].arrival_t.day, flights[write_counter].arrival_t.hour, flights[write_counter].arrival_t.minute);
}
fclose(fpw);
return 0;
}

/* Loads flight details from the text file 'database.txt */
int read_flight(flight_t flights[], int counter)
{
int read_counter;
FILE* fpr = NULL;
fpr = fopen(DB_NAME, "r");
if (fpr == NULL)
{
printf("Read error ");
return 1;
}
fscanf(fpr, "%d", &counter);
for (read_counter=0; 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);
return 0;
}

Explanation / Answer

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

/*******************************************************************************

* 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"

/*******************************************************************************

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

/*******************************************************************************

* 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[]);

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;

int flag=1,status,temp;

flight_t flights[MAX_NUM_FLIGHTS];

do

{

fflush(stdin);

print_menu();

while (flag)

{

status = scanf("%d", &choice);

if((choice>=1 && choice<=5) && status==1)

flag=0;

else

{

while((temp=getchar()) != EOF && temp != ' ');

system("CLS");

print_menu();

}

}

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

{

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;

}

/*******************************************************************************

* 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[])

{

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 {

printf("Invalid input ");

}

} while (12 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 {

printf("Invalid input ");

}

} while (12 }

/* 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;

}

/* Displays all flight details inputted by the user

void print_flights_all(int counter, flight_t flights[counter])

{

int second_counter;

printf("Flight Origin Destination ");

printf("------ --------------- --------------- ");

for (second_counter=0; second_counter {

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

}

}

/* Displays current flight details indicated by the flightcode

void print_flight(int counter, flight_t flights[], char second_choice[MAX_FLIGHTCODE_LEN+1])

{

int second_counter;

printf("Flight Origin Destination ");

printf("------ --------------- --------------- ");

for (second_counter=0; second_counter<1; second_counter++)

{

if (strncmp(second_choice, flights[second_counter].arrival_city, 4)==0)

{

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

}

}

}

/* Saves flight details in seperate text file called 'database.txt

int write_flight(flight_t flights[], int counter)

{

int write_counter;

FILE* fpw = NULL;

fpw = fopen(DB_NAME, "w");

if (fpw == NULL)

{

printf("Read error ");

return 1;

}

fprintf(fpw, "%d", counter);

for(write_counter=0; write_counter {

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

}

fclose(fpw);

return 0;

}

/* Loads flight details from the text file 'database.txt

int read_flight(flight_t flights[], int counter)

{

int read_counter;

FILE* fpr = NULL;

fpr = fopen(DB_NAME, "r");

if (fpr == NULL)

{

printf("Read error ");

return 1;

}

fscanf(fpr, "%d", &counter);

for (read_counter=0; 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);

return 0;

}*/

updated code please check it now. its working and please remove dilike .

see untill you don't enter the no between 1 to 5 it will do nothng when you enter bo between 1 to 5 it will go to the respetive case.

if you have any query regarding the code please ask me in the commnet , i am here for the help.

We were unable to transcribe this image

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote