我正在尝试编写一个数据验证函数,其中 ID 号必须是结构数组中的唯一编号。每次我尝试保存有效的 ID 号时,程序都会崩溃 - 为什么会发生这种情况?
这是结构,以及结构数组的声明:
struct customer { // set up customer template //
char name [MAXNAM];
char surname [MAXNAM];
int idnum [MAX_ID];
};
struct customer data_cus[MAXCUS];
这是在主程序中:
printf ("Please Enter ID Card, [NOTE! Only numbers are allowed!]\n");
int checkID;
while ((scanf ("%d",&checkID) == 0 || customerID(checkID, count)==1))
{
printf ("This ID is already taken! Please enter unique ID!\n");
while (getchar()!='\n')
{
continue;
}
*data_cus[count].idnum = checkID;
}
这是用于检查文件中已存在的 ID 的函数:
int customerID (int cCheck, int count)
{
FILE * pcustomer;
int size = sizeof (struct customer);
struct customer temp;
rewind (pcustomer);
while (fread (&temp,size,count,pcustomer)==1)
{
if (*temp.idnum == cCheck)
{
return 1;
}
}
return 0;
}
谢谢您的帮助!