gpt4 book ai didi

c - 呃....我是瞎子吗?似乎找不到 C 编程错误

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

嗯,我正在尝试完成一些编码......我不确定我的程序到底出了什么问题,错误似乎出现在输出区域,因为我认为它给了我变量的内存位置,但我初始化为 0,我不确定现在可能出了什么问题......困惑的表情。这是程序(只是错误区域,不是完整的程序)。

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

typedef struct Mica
{
char FleNme [256];
}Mscl;

typedef struct Blade
{
char BladeName [80];
}blade;

typedef struct eader
{
char Header[256];
}header;

int main()
{
Mscl Access;
blade info;
int hcount =0;
int x=0;
char decision;
float BladePrice = 0;
int spin = 0;
int speed= 0;
int weight= 0;
header inf[hcount];
FILE *Nmes;

system("cls");

printf("Please enter the name of the file you wish to Add:\n ");
scanf("%s",&Access.FleNme);

if((Nmes=fopen(Access.FleNme,"r"))==NULL)
{
printf("File can be created\n");
Nmes=fopen(Access.FleNme,"w");
}
else
{
printf("File exist Already redirecting to Menu y for yes n for no: ");
scanf("%s",&decision);

if(decision=='y')
{
main();
}
else
{
exit(0);
}
}

printf("Format Example: Bladename BladePrice Spin Control Weight\n");
printf("Please Input how many headers you will be using: ");
scanf("%d",&hcount);

for (x=0;x<hcount;x++)
{
printf("Please enter The Header Name: ");
scanf("%s",&inf[x].Header);
}

system("cls");
printf("Please enter Name of Item: ");
scanf("%s",&info.BladeName);
printf("\nPlease enter Price of Item: ");
scanf("%f",&BladePrice);
printf("\nPlease enter Speed of Item: ");
scanf("%d",&speed);
printf("\nPlease enter Spin of Item: ");
scanf("%d",&spin);
printf("\nPlease enter weight of Item: ");
scanf("%d",&weight);

for(x=0;x<hcount;x++)
{
printf("%s\t",inf[x].Header);
}

printf("\n%s\t%.2f\t%d\t%d\t%d",&info.BladeName,&BladePrice,&speed,&spin,&weight);
}

最佳答案

一个错误是

char decision;
...
scanf("%s",&decision);

您要求 scanf 将字符数组写入单个字符。这将产生未定义的结果,但很可能导致决策之后的堆栈变量被覆盖。

要解决此问题,请使用 %c 作为格式说明符

scanf("%c",&decision);

或将decision声明为字符数组

char decision[32];
scanf("%.31s",decision);

如果您仍然发现其他问题,请更具体地说明这些问题在您的程序中发生的位置。如果您花时间格式化代码并修复其缩进,这也会有很大帮助。

关于c - 呃....我是瞎子吗?似乎找不到 C 编程错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14627767/

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