检查重复条目不起作用

标签 c boolean duplicates

我目前正在创建 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/

相关文章:

java - 简化哈希集重复检测器的代码

python - 如何运行 Python 文件作为 C 文件的参数?

c - 当将 char 指针指向的内存类型转换为无符号整数时,传入的值与预期不符

在 R 中逐行删除重复值

php - 当我将 boolean 值 True-False 作为绑定(bind)到 int 字段的参数发送到 PDO 语句时会发生什么?

JavaScript 逻辑搜索?

mysql 在重复的 FIELD 而不是 KEY 上插入

c++ - 在运行程序之前加载/注册 DLL

c - Linux内核模块: Problem with kernel_write function

arrays - 删除 Swift 数组中元素的最后一次出现