gpt4 book ai didi

c - 字符串在执行中途丢失数据 C

转载 作者:行者123 更新时间:2023-11-30 19:47:42 24 4
gpt4 key购买 nike

在进行航空预订系统时,输入新航类时一切正常,除了未保存的到达位置之外。我已经完成了 printf 测试,以查看变量在哪里丢失其存储的数据。我添加了注释来帮助您,并删除了一些不必要的 printfs 和注释以使代码更简单。

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

delete 变量始终保持良好状态;

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

最佳答案

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

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

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

这会导致缓冲区溢出,因为 codeAcodeD 是 3 个字符串 + 一个空终止字符,因此 arrivedepart 尺寸至少需要为 4。

关于c - 字符串在执行中途丢失数据 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20818065/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com