我目前正在创建 C 控制台程序。在我的程序中,我可以添加和编辑我的记录,我决定检查记录中每个 ID 的重复条目。我正在使用 boolean 值来检查重复的条目。我现在面临的问题是我的记录与之前的条目具有相同的数据。
typedef struct People
{
char code[100],name[100];
int age,height,weight;
}People;
typedef int bool;
#define true 1
#define false 0
void add(char* code, char* name, int* age, int* height, int* weight)
{
bool is_matched = false;
char searchCode[100];
printf("Enter code name age height weight:");
scanf("%s %s %d %d %d",code, name, &age, &height, &weight);
/* Check for Duplicated Entry Below*/
}
int main()
{
People person;
add(&person.code,&person.name,&person,age,&person.height,&person.weight);
return 0;
}
检查是否有重复条目
while(fscanf(fp1,"%s %s %d %d %d",searchCode, name, &age, &height, &weight) == 5)
{
if(strcmp(searchCode,code) == 0)
{
printf("Model Code already exist in the records.\n");
printf("Please try different model code.\n");
is_matched = true;
break;
}
}
if(!is_matched){
fprintf(fp1,"%s %s %d %d %d\n",code,name,age,height,weight);
}
最佳答案
您阅读的内容不正确!
当你说:
scanf("%d", second_parameter);
第二个参数应该是地址,这就是为什么我们使用运算符 & 来提取它所应用的变量的地址。
您的函数 header 是:
void add(char* code, char* name, int* age, int* height, int* weight)
所有参数都是指针,当您调用 scanf 时,您将获取该指针的地址(而不是它指向的变量的地址),我建议您:
1 - 将 scanf 语句更改为:
scanf("%s %s %d %d %d",code, name, age, height, weight);
2 - 将函数调用更改为:
add(person.code,person.name,&person,age,&person.height,&person.weight);
也更改您的 fscanf...然后再次运行它,看看它是否有效
关于检查重复条目不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24319507/