Question

While making an airline reservation system everything works well when entering a new flight, apart from the arrival location which is not being saved. I have done printf tests to see where the variable is losing its stored data. I have placed comments to help you and taken out some unnecessary printfs and comments to make the code more simple.

I think that the variable scope is OK however I could be mistaken.

The depart variable remains fine throughout;

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>
#include "Airline.h"

static int curFly = 0;
struct Flight flyList[30];

void newFlight ()
{
    printf("\n");
    printf("============= CREATE A NEW FLIGHT ============= \n");
    printf("\n");

    // FLIGHT ID or CANCEL

    printf("EnterFlight ID (0 to cancel) : ");
    int tempID;
    if (1 != scanf("%d", &tempID)){
        do{
            printf("Invalid Entry... Enter Flight ID: ");
            fflush(stdin);
        } while (1 != scanf("%d", &tempID));
    } else {
        if (tempID == 0){
            printf("\n");
            printf("Entry Canceled: Redirecting You to Main Menu \n");
            printf("\n");
            mainMenu();
        } else {
            flyList[curFly].flightID = tempID;
        }
    }

    // DESTINATION

    printf("Enter Destination (Airport Code): ");
    char codeA[4];
    fflush(stdin);
    scanf("%3s", codeA);

    if (!((strncmp(codeA, "MLA", 3) == 0) || (strncmp(codeA, "LGW", 3) == 0) || (strncmp(codeA, "LHR", 3) == 0) || (strncmp(codeA, "ORK", 3) == 0) || (strncmp(codeA, "MUC", 3) == 0) || (strncmp(codeA, "FRA", 3) == 0) || (strncmp(codeA, "CDG", 3) == 0))){
        do{
            printf("Invalid Entry... Enter 3 Letter Airport Code: ");
            fflush(stdin);
            scanf("%3s", codeA);
        } while (!((strncmp(codeA, "MLA", 3) == 0) || (strncmp(codeA, "LGW", 3) == 0) || (strncmp(codeA, "LHR", 3) == 0) || (strncmp(codeA, "ORK", 3) == 0) || (strncmp(codeA, "MUC", 3) == 0) || (strncmp(codeA, "FRA", 3) == 0) || (strncmp(codeA, "CDG", 3) == 0) ));
    }   

    strcpy(flyList[curFly].arrive, codeA);
//COPIES codeA to corresponding var in flight struct
// STAYS valid until using strcpy(flyList[curFly].depart, codeD); 
//Look for **

    //DEPARTURE

    printf("Enter Place of Departure (Airport Code): ");
    char codeD[4];
    fflush(stdin);
    scanf("%3s", codeD);

    if (!((strncmp(codeD, "MLA", 3) == 0) || (strncmp(codeD, "LGW", 3) == 0) || (strncmp(codeD, "LHR", 3) == 0) || (strncmp(codeD, "ORK", 3) == 0) || (strncmp(codeD, "MUC", 3) == 0) || (strncmp(codeD, "FRA", 3) == 0) || (strncmp(codeD, "CDG", 3) == 0) || (strncmp(codeD, codeA, 3) == 0))){
        do{
            printf("Invalid Entry... Enter 3 Letter Airport Code: ");
            fflush(stdin);
            scanf("%3s", codeD);
        } while (!((strncmp(codeD, "MLA", 3) == 0) || (strncmp(codeD, "LGW", 3) == 0) || (strncmp(codeD, "LHR", 3) == 0) || (strncmp(codeD, "ORK", 3) == 0) || (strncmp(codeD, "MUC", 3) == 0) || (strncmp(codeD, "FRA", 3) == 0) || (strncmp(codeD, "CDG", 3) == 0) || (strncmp(codeD, codeA, 3) == 0)));

    }   
    //OK TILL HERE
    //**
    strcpy(flyList[curFly].depart, codeD);
    //HERE flyList[curFly].arrive turns blank


    //DEPARTURE DATE

    printf("Enter Date Of Departure (DD MM YYYY): ");
    short d;
    short m;
    short y;
    if ((3 != scanf("%hd %hd %hd", &d, &m, &y)) || !(m <= 12) || !(y >= CURRYR) || !(y <= 2020)|| !(((d <= 29) && (m == 2)) || ((d <= 31) && ((m == 1) || (m == 3) || (m == 5) || (m == 7) || (m == 8) || (m = 10) || (m == 12))) || ((d <= 30) && ((m == 4) || (m == 6) || (m == 9) || (m == 11))))){
        do {
            printf("Please enter a valid Date: ");
            fflush(stdin);
        } while ((3 != scanf("%hd %hd %hd", &d, &m, &y)) || !(m <= 12) || !(y >= CURRYR) || !(y <= 2020)|| !(((d <= 29) && (m == 2)) || ((d <= 31) && ((m == 1) || (m == 3) || (m == 5) || (m == 7) || (m == 8) || (m = 10) || (m == 12))) || ((d <= 30) && ((m == 4) || (m == 6) || (m == 9) || (m == 11)))));
    }

    flyList[curFly].timeOfDep.day   = d;
    flyList[curFly].timeOfDep.month = m;
    flyList[curFly].timeOfDep.year  = y;

    // FLIGHT TIME

    printf("Enter Time Of Departure (HH MM)in 24Hr Format: ");
    short hr;
    short min;
    if ((2 != scanf("%hd %hd", &hr, &min)) || (min > 60) || (hr > 23)){
        do {
            printf("Please enter a valid time: ");
            fflush(stdin);
        } while ((2 != scanf("%hd %hd", &hr, &min)) || (min > 60) || (hr > 23));
    }

    flyList[curFly].timeOfDep.hour   = hr;
    flyList[curFly].timeOfDep.minute = min;

    printf("\n");
    printf("\n");

    printf("Please Confirm Flight %d \n", flyList[curFly].flightID);
    printf("To fly from %s to %s \n", flyList[curFly].depart, flyList[curFly].arrive);
    printf("On %hd/%hd/%hd at %hd:%hd \n", flyList[curFly].timeOfDep.day, flyList[curFly].timeOfDep.month, flyList[curFly].timeOfDep.year, flyList[curFly].timeOfDep.hour, flyList[curFly].timeOfDep.minute);
    printf("Y for Yes, N for No: ");
    char con;
    if(1 != scanf(" %c", &con) || !((con == 'y') || (con == 'Y') || (con == 'n')|| (con == 'N'))){
        do {
            printf("Invalid Entry, Press Y for Yes, N for No: ");
            fflush(stdin);
        } while (1 != scanf(" %c", &con)|| !((con == 'y') || (con == 'Y') || (con == 'n')|| (con == 'N')));
    }

    if (con == 'y' || con == 'Y'){
        curFly++;
        printf("Thank You, Flight saved, Redirecting you to main Menu \n");
        printf("\n");
        mainMenu();
    } else {
        printf("Thank You, Entry Canceled, Redirecting you to main Menu \n");
        printf("\n");
        mainMenu();
    }

}

void editFlight ()
{

}

void cancelFlight ()
{

}

Here is the Flight structure in Airline.h

struct Flight {

    int flightID;
    char depart[3];             //Stores the 3 Letter code for the airport 
    char arrive[3];
    float costFC;                       //Cost to fly first class
    float costE;                        //Cost to fly Economy
    struct Passenger passFC[MAXFC];     //Array of passengers booked in first class
    struct Passenger passE[MAXE];       //Array of passengers booked in economy
    struct DateTime timeOfDep;
    struct DateTime timeOfArr;
}
Was it helpful?

Solution

char depart[3];
char arrive[3];

Need to be char[4]. A character is needed for the null terminated character.

strcpy(flyList[curFly].depart, codeD);    
strcpy(flyList[curFly].arrive, codeA);

this results in buffer overrun as codeA and codeD are 3 character string + a null terminated character, so arrive and depart need to be at least size 4.

OTHER TIPS

You need to make sure codeA and codeD are null terminated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top