gpt4 book ai didi

c - 即使函数不是静态的,第二次运行也会出错

转载 作者:行者123 更新时间:2023-11-30 17:03:43 27 4
gpt4 key购买 nike

我已经创建了一个建筑物的功能,现在在第二个建筑物上创建了两个建筑物,它由于某种原因跳过了一些参数......为什么?现在我检查了代码几次,我没有使用任何静态变量,所以它不应该这样做......那么为什么它会记住第二次运行的时间?

更新我按照所说的做了,但现在它只返回 false...任何帮助都会有所帮助

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//defines
#define SIZE 30
#define Q_SIZE 4
#define TRUE 1
#define FALSE 0
struct shape
{
char name[SIZE]; /* name of the shape */
int edgeNum; /* number of edges */
char sPol[Q_SIZE]; /* if the shape is a regular polygrom */
};
int shapeMaker();
int main(void)
{
int check;
check = shapeMaker();
if (check == TRUE)
{
printf("the two shapes are equals!\n");
}
else if (check == FALSE)
{
printf("the two shapes are not equals!\n");
}
system("PAUSE");
return 0;
}

int shapeMaker()
{
struct shape firstShape;
struct shape secondShape;

printf("please enter the first shape name: ");
fgets(secondShape.name, SIZE, stdin);
secondShape.name[strcspn(secondShape.name, "\n")] = 0;
printf("\nnow please enter the number of Edges: ");
scanf("%d", &secondShape.edgeNum);
printf("\nis the shape is regular polygom (yes/no)? ");
scanf("%s", secondShape.sPol);
getchar();
printf("\nplease enter the second shape name: ");
fgets(secondShape.name, SIZE, stdin);
secondShape.name[strcspn(secondShape.name, "\n")] = 0;
printf("\nnow please enter the number of Edges: ");
scanf("%d", &secondShape.edgeNum);
printf("\nis the shape is regular polygom (yes/no)? ");
scanf("%s", secondShape.sPol);
if (firstShape.name == secondShape.name && firstShape.edgeNum == secondShape.edgeNum && firstShape.sPol == secondShape.sPol)
{
return TRUE;
}
else
{
return FALSE;
}
}

最佳答案

在 shapeMaker 开头和 fgets 之后添加 fflush(stdin);

关于c - 即使函数不是静态的,第二次运行也会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36043410/

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