字符串在执行C的一半时丢失数据

时间:2013-12-28 18:45:28

标签: c string variables struct strcpy

在建立航空公司预订系统时,除了未保存的到达地点外,一切都在进入新航班时运作良好。我已经完成了printf测试,以查看变量丢失其存储数据的位置。我已经发表了评论来帮助你并删除一些不必要的printfs和注释,以使代码更简单。

我认为变量范围可以,但我可能会弄错。

离开变量始终保持良好状态;

#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 ()
{

}

这是Airline.h中的Flight结构

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

2 个答案:

答案 0 :(得分:1)

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

需要成为char [4]。空终止字符需要一个字符。

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

这导致缓冲区溢出为codeAcodeD为3个字符串+空终止字符,因此arrivedepart必须至少为4。

答案 1 :(得分:-1)

您需要确保codeA和codeD为空终止。