gpt4 book ai didi

c - 存储用户输入的同时仍然提出问题

转载 作者:行者123 更新时间:2023-11-30 14:49:20 26 4
gpt4 key购买 nike

首先,我知道下面的源代码很长,你实际上不应该发布这样的代码,但我真的不明白为什么它不起作用,或者我如何在不这样发布的情况下解释我的问题。

我试图存储其中提出的每个问题的答案并将其显示在脚本的末尾。我遇到的最大问题是我收到错误
“下标值既不是数组,也不是指针,也不是 vector scanf("%s", a[i].incdest);”

程序不接受a[i].incdest。它对所有数组值执行此操作。

这也表明在函数中主变量“comp1”未声明。

#include<stdio.h>
#include<string.h>

int c_s(char*, char*);

typedef struct
{
char constab[30];
char vicwit[15];
char witdet[200];
char incdest[300];
char comp1[15];
char comp2[200];;
} sheetstore;

#define ARRAYLEN 2

sheetstore a[ARRAYLEN];
FILE *fp;

int main()
{
int i, a;
char wit[10] = "witness";
char yes[10] = "yes";
char comp1[10];

fp = fopen("sheetstore.dat","a+");

printf("Hate crime reporting system\n\n\n");

printf("If the crime you are reporting is an emergency,\nplease call 999, do not proceed any further with this form\n\n\n\nPlease press enter to confirm you have read the above and continue\n");
char enter = 0;
while (enter != '\r' && enter != '\n') { enter = getchar(); }

for( i=0; i<ARRAYLEN ; i++)
{
printf("Which police constabulary did the offence take place in?\n\n");
scanf("%s", a[i].constab);
printf("Are you a victim or witness of the crime?\nPlease answer victim/witness\n\n");
scanf("%s", a[i].comp1);
int res1 = (strcmp (comp1, wit));
if(res1 == 0){
printf("Please enter the details including phone number and address of any other witnesses that were present\n");
}
scanf("%s", a[i].witdet);
else{
printf("Where did the incident take place?\nIf in a house please provide the full address including postcode\n");
scanf("%s", a[i].incdest);
}

fwrite(&a[i], sizeof(a), 1, fp);
}
fclose(fp);

fopen("sheetstore.dat", "r");
for(i=0; i<ARRAYLEN; i++)
{
fread(&a[i], sizeof(a), 1, fp );
printf("Which police constabulary did the offence take place in? : %s\n", a[i].constab);
printf("Are you a victim or witness of the crime? : %s\n", a[i].comp1);
printf("Please enter the details including phone number and address of any other witnesses that were present : %s\n", a[i].witdet);
printf("Where did the incident take place? : %s\n", a[i].incdest);
}
fclose(fp);

return 0;
}

最佳答案

我在您的代码中看到的主要问题是您隐藏了数组 a ( sheetstore a[ARRAYLEN]; ) 通过声明 int在您的main()中具有相同的名称。

你还有一个scanf语句错位。

我修复了你的代码并为每个更改添加了注释 - 现在,我不想检查你的代码的功能,但至少这会编译,并希望能让你更好地理解你错在哪里 - 来自这取决于你:

#include<stdio.h>
#include<string.h>

int c_s(char*, char*);

typedef struct
{
char constab[30];
char vicwit[15];
char witdet[200];
char incdest[300];
char comp1[15];
char comp2[200];;
} sheetstore;

#define ARRAYLEN 2

sheetstore sheetArr[ARRAYLEN]; //change this 'a' to avoid shadowing by the decleration on 'int a' in main
FILE *fp;

int main()
{
int i, a;
char wit[10] = "witness";
char yes[10] = "yes";
char comp1[10];

fp = fopen("sheetstore.dat","a+");

printf("Hate crime reporting system\n\n\n");

printf("If the crime you are reporting is an emergency,\nplease call 999, do not proceed any further with this form\n\n\n\nPlease press enter to confirm you have read the above and continue\n");
char enter = 0;
while (enter != '\r' && enter != '\n') { enter = getchar(); }

for( i=0; i<ARRAYLEN ; i++)
{
printf("Which police constabulary did the offence take place in?\n\n");
scanf("%s", sheetArr[i].constab);//change name from 'a'
printf("Are you a victim or witness of the crime?\nPlease answer victim/witness\n\n");
scanf("%s", sheetArr[i].comp1);//change name from 'a'
int res1 = (strcmp (sheetArr[i].comp1, wit));//compare with the value set in the struct
if(res1 == 0){
printf("Please enter the details including phone number and address of any other witnesses that were present\n");
/*this scanf should be inside the brackets of 'if(res1 == 0)' */
scanf("%s", sheetArr[i].witdet);//change name from 'a'
}
else{
printf("Where did the incident take place?\nIf in a house please provide the full address including postcode\n");
scanf("%s", sheetArr[i].incdest);//change name from 'a'
}

fwrite(&sheetArr[i], sizeof(sheetstore), 1, fp);//write a single struct
}
fclose(fp);

fopen("sheetstore.dat", "r");
for(i=0; i<ARRAYLEN; i++)
{
fread(&sheetArr[i], sizeof(sheetstore), 1, fp );//read a single struct
printf("Which police constabulary did the offence take place in? : %s\n", sheetArr[i].constab);
printf("Are you a victim or witness of the crime? : %s\n", sheetArr[i].comp1);
printf("Please enter the details including phone number and address of any other witnesses that were present : %s\n", sheetArr[i].witdet);
printf("Where did the incident take place? : %s\n", sheetArr[i].incdest);
}
fclose(fp);

return 0;
}

关于c - 存储用户输入的同时仍然提出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49647858/

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