This is a flight booking program.
The program is supposed to match what the user enters to what is stored in a file. It works fine but only for correct data,I need a way to go back to the main menu if the user enters the wrong values example if the user enters the wrong country code or flight date. Data is matched in the comparefirst function at the bottom.
#include <stdio.h>
#include <windows.h>
#include <stdlib.h> /*use of malloc function*/
#include <string.h>
#define BOOK_FLIGHT '1'
#define CHECK_FLIGHT '2'
#define QUIT '3'
#define MAXSTRINGSIZE 100
#define MAXARRAYSIZE 100
typedef char String[MAXSTRINGSIZE];
char Menu1()
{
printf("CUB-CUBA\n");
printf("BAR-BARBADOS\n");
printf("GRE-GRENADA\n");
printf("STV-ST.VINCENT\n");
printf("STL-ST.LUCIA\n");
printf("STK-ST. KITTS AND NEVIS\n");
printf("ARU-ARUBA\n");
printf("PUE-PUERTO RICO\n");
printf("HAI-HAITI\n");
printf("BAH-BAHAMAS\n");
printf("JAM-JAMAICA\n");
printf("DOM-DOMINICA\n");
}
char mainMenu(void);
typedef struct
{
String flight_num;
String destination;
String dep_date; String dep_time;
int EC_seats; float EC_price;
int FC_seats; float FC_price;
}Flights;
int loadFlights(Flights* FDA[]);
void printFlights(Flights *newFlights);
int booking(Flights* FDA[]);
int comparefirst(Flights *FDA[], String destination, String dep_date, int FC_seats);
int main()
{
}
int booking(Flights *FDA[])
{
String destination;
String dep_date;
String seatclass;
String flight_num;
int EC_seats;
int FC_seats;
int Num_persons;
float Price;
String Name;
String ID_num;
char first[10] = "FC";
char econ[10] = "EC";
char eandf[10]= "FC&EC";
printf("Chose a destination by entering a country code below\n");
printf("\n");
Menu1();
scanf("%s",destination);
printf("Choose flight date dd/mm/yy format\n");
scanf("%s",dep_date);
printf("Choose travel class by entering FC for first class,EC for economy class or FC&EC for both\n");
scanf("%s",seatclass);
if (strcmp(seatclass,first)==0)
{
printf("\nHow many persons are traveling in first class");
scanf("%d", &FC_seats);
comparefirst(FDA,destination,dep_date,FC_seats);
}
}
int loadFlights(Flights* FDA[])
{
int EC_seats,FC_seats;
float EC_price,FC_price;
String flight_num,destination,dep_date,dep_time;
FILE *fp;
fp = fopen("Flightinfo.txt.txt","r");
if(fp==NULL)
{
printf("\nFile does not exist or is corrupted please re-enter file name");
return -1;
}
int count=0;
while(!feof( fp))
{
Flights* newFlights;
fscanf( fp,"%s",flight_num);fgetc(fp); fscanf( fp,"%s",destination);fgetc(fp); fscanf( fp,"%s %s",&dep_date,&dep_time);fgetc(fp);
fscanf( fp,"%d %f",&EC_seats,&EC_price);fgetc(fp);
fscanf( fp,"%d %f",&FC_seats,&FC_price);fgetc(fp);
newFlights = (Flights*)malloc(sizeof(Flights));
strcpy(newFlights-> flight_num,fixString(flight_num));
strcpy(newFlights-> destination,fixString(destination));
strcpy(newFlights-> dep_date,fixString(dep_date));
strcpy(newFlights-> dep_time,fixString(dep_time));
newFlights-> EC_seats = EC_seats;
newFlights-> EC_price = EC_price;
newFlights-> FC_seats = FC_seats;
newFlights-> FC_price = FC_price;
FDA[count]=newFlights;
printFlights(FDA[count]);
count++;
}
return count;
}
char *fixString(String line)
{
for (int i=0; i<maxstringsize;> {
if (line[i]=='\n')
{
line[i]='\0';
break;
}
}
return line;
}
void printFlights(Flights *newFlights)
{
printf("////////////FLIGHT/////////////\n");
printf( "%s\n",newFlights->flight_num);
printf( "%s\n",newFlights->destination);
printf( "%s %s\n",newFlights->dep_date,newFlights->dep_time);
printf( "%d %.02f\n",newFlights->EC_seats,newFlights->EC_price);
printf( "%d %.02f\n",newFlights->FC_seats,newFlights->FC_price);
printf("\n\n");
}
int comparefirst(Flights *FDA[], String destination, String dep_date, int FC_seats)
{
float Price;
int j=0;
for (int i=0;i<maxarraysize;i++)>
{
if (FDA[i]==NULL)
{
i++; }
else if(strcmp(FDA[i]->dep_date,dep_date)==0 && (strcmp(FDA[i]->destination,destination)==0) && (FC_seats<=FDA[i]->FC_seats))
{
printFlights(FDA[i]);
Price=(FC_seats*FDA[i]->FC_price);
printf("Total cost of flight is $%.02f\n",Price);
getchar();
j++;
if(j >= 10)
{
break; }
}
else if(strcmp(FDA[i]->dep_date,dep_date)==0 && (strcmp(FDA[i]->destination,destination)==0) && (FC_seats>FDA[i]->FC_seats))
{
printf("Not enough first class seats");
getchar();
getchar();
j++;
if(j >= 10)
{
break; }
}
}
return j; }