gpt4 book ai didi

c - 值同步无法正常工作

转载 作者:行者123 更新时间:2023-11-30 16:48:11 25 4
gpt4 key购买 nike

我的程序的此链接中有完整的代码;请阅读本文,我这里的代码足够长,但不是全部:https://pastebin.com/NpspcJB8

作为我评估的一部分,我试图让我的代码将一个数组索引与另一个数组索引“同步”。出现的问题是,当我重复运行部分代码时,输​​入值后输出不会改变,例如:

case 'b':

CaseBPrompt();

for (t=0;t<350;t++) {
int result2[350];
result2[t] = strcmp(blankSpace, parkingSpace[t]); // determine if the value of parkingSpace is the same as strcmp "empty"
Synchronise(t);

if (result2[t] == 0) { // if value of parkingSpace == "empty"
// use spaceNumber; sync int variable w/array index
LevelPrint(t);

strcpy(carCustomer[t], customerName);
// copy value of car number to index
strcpy(parkingSpace[t], carNumber); // omit array index to ensure this can run
break; // required, as will print other available spaces otherwise
}

}
printf("You will take %s's car\n", carCustomer[t]);
printf("Please drive the car over to %c%d\n", level, spaceNumber[t][3]);
break;

t 是我在 main 外部全局声明的一个整型变量。以下是我这里用到的函数内容:

void CaseBPrompt() {
printf("You have chosen to park a customer's car \n");
printf("Please input the customer's name, using _ in lieu of a space\n");
scanf(" %s", &customerName);
// assign customer's name to carNumber - new array?

printf("Please input the values of the number plate\n");
// ensure use %s, as collecting string, and so const char/char error won't occur
scanf(" %s", &carNumber);

}
void LevelPrint(int n) {
if (spaceNumber[n][3] <= 100) {
level = 'A';
// don't forget to write out complete matrix for spaceNumber!

printf("There is an available space at parking space %c%d\n", level,spaceNumber[n][3]);
// assign parkingSpace array index w/user input

} else if (spaceNumber[n][3] > 100 && spaceNumber[n][3]<= 200) {
level = 'B';
spaceNumber[n][3]-=100; // display correct parking space at corresponding level
printf("There is an available space at parking space %c%d\n", level,spaceNumber[n][3]);

} else if (spaceNumber[n][3] > 200 && spaceNumber[n][3]<= 300) {
level = 'C';
spaceNumber[n][3]-=200; // display correct parking space at corresponding level
printf("There is an available space at parking space %c%d\n", level,spaceNumber[n][3]);

} else if (spaceNumber[n][3] > 300 && spaceNumber[n][3]<= 350) {
level = 'D';
spaceNumber[n][3]-=300; // display correct parking space at corresponding level
printf("There is an available space at parking space %c%d\n", level,spaceNumber[n][3]);

}
}
void Synchronise(int a) {
spaceNumber[a][3] = n+1; // synchronise w/parking space
carCustomer2[a][3] = n+1; // synchronise w/car customer name
}

当我运行代码时,我收到以下输出:

empty
350
25
Please write car number (without spaces):
b

b
Valet options (press the corresponding letter to select):
a) Check available spaces
b) Park a customer's car
c) Retrieve a customer's car
q) Exit the program
Your choice: b
You have chosen to park a customer's car
Please input the customer's name, using _ in lieu of a space
b
Please input the values of the number plate
b
There is an available space at parking space A1
You will take b's car
Please drive the car over to A1
Valet options (press the corresponding letter to select):
a) Check available spaces
b) Park a customer's car
c) Retrieve a customer's car
q) Exit the program
Your choice: b
You have chosen to park a customer's car
Please input the customer's name, using _ in lieu of a space
b
Please input the values of the number plate
b
There is an available space at parking space **A1**
You will take b's car
Please drive the car over to **A1**
Valet options (press the corresponding letter to select):
a) Check available spaces
b) Park a customer's car
c) Retrieve a customer's car
q) Exit the program
Your choice: c
You have chosen to retrieve a customer's car
Please write their name, again with _ in lieu of a space:
c
Please write down what was on their number plate:
90jda
c's car is at **1**.
Valet options (press the corresponding letter to select):
a) Check available spaces
b) Park a customer's car
c) Retrieve a customer's car
q) Exit the program
Your choice:

输出中用星号包围的字符是我所关心的,因为在 case 'c' 下添加代码之前,case b 下的代码会正确打印。我正在使用 DevC++,我想知道我的代码是否有问题。

当我再次选择执行选项 B 时,代码会为之前的每次迭代打印 A2、A3 等,并且选项 C 输出下显示的值是 149,因为我预先分配了一个值parkingSpace[150][3] 下的 90jda

parkingSpace 下的值旨在与 case 'b'blankSpace 同步,并打印出其中的可用空间。 for 循环。对于 case 'c'parkingSpace 旨在与保存与用户输入 carNumber 中的字符串相同的任何索引进行同步,该字符串在case 'c'for 循环的开头。

最佳答案

我相信我成功解决了这个问题,这是因为我在数组中声明的值之一不相同;程序没有返回错误,因为两者都被声明为整型变量

关于c - 值同步无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43054850/

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