检查重复的条目是否有效

时间:2014-06-20 03:34:58

标签: c boolean duplicates

我目前正在创建C控制台程序。在我的程序中,我可以添加和编辑我的记录,我决定检查我记录中每个id的重复输入。我使用布尔来检查重复的条目。我现在面临的问题是我的记录与前一个条目具有相同的数据。

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);
}

1 个答案:

答案 0 :(得分:0)

你正在错误地阅读你的东西!

当你说:

 scanf("%d", second_parameter);

这个第二个参数应该是一个地址,这就是为什么我们使用operator&,来提取它所应用的变量的地址。

您的功能标题是:

 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 ...然后再次运行它,看它是否有效

相关问题