Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

C++
#include <stdio.h>
#include <windows.h>
#include <stdlib.h> /*use of malloc function*/
#include <string.h>

/*Using symbolic constants*/
#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()
{

}//end of 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;

//open a file for reading only
FILE *fp;
fp = fopen("Flightinfo.txt.txt","r");

//test to see if file exist
if(fp==NULL) 
{
    printf("\nFile does not exist or is corrupted please re-enter file name"); 
    return -1;
}

int count=0;
//using a loop to read all the records in the file 
while(!feof( fp))
{
   //declear a new stucture to store flights
   Flights* newFlights;

   fscanf( fp,"%s",flight_num);fgetc(fp);//get eoln char
   fscanf( fp,"%s",destination);fgetc(fp);//get eoln char
   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;

   //no place in the array 
   FDA[count]=newFlights;

   //printing the flight that was just added to the array
   printFlights(FDA[count]);
   //update count
   count++;
}//end while

return count;
}//end of load 

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++)>
    {
        //test for null

        if (FDA[i]==NULL) 
        { 
            i++;//skip over this flight record         
        }
        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; // Stop Looping as Array is full
            }
        }
        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; // Stop Looping as Array is full
            }
        }
    }  
   return j; // Return Count of the Flights found.
}
Posted
Updated 14-Apr-15 20:18pm
v2
Comments
Sergey Alexandrovich Kryukov 15-Apr-15 0:32am    
Did you look at you own code after you posted it? It says
#include
#include...
Please fix it, and use pre element around it.
—SA

1 solution

This is easily accomplished implementing a simple Finite State Machine, see this Wikipedia page: "Finite-state machine"[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900