首页 > 解决方案 > 它只显示一个酒店预订

问题描述

我正在预订酒店,所以我尝试使用用户插入的 iD 显示预订,但我无法得到它,程序只是冻结而没有错误或任何我尝试了很多东西并在网上查找找不到回答。我尝试在 if 循环中做一段时间,它也没有工作,无法弄清楚问题出在哪里。谢谢。

void display(char *a)
{
    FILE *fp;
    rec = count;
    int choice;
    fp = fopen(a ,"r");
    struct  hotelStruct *temp = (struct hotelStruct *)malloc(sizeof(struct hotelStruct));
    temp->first_name = (char *)malloc(10* sizeof(char));
    temp->last_name = (char *)malloc(15*sizeof(char));
    temp->passport = (char *)malloc(15*sizeof(char));
    temp->nationality = (char *)malloc(30*sizeof(char));
    temp->room = (char *)malloc(10*sizeof(char));
    temp->email = (char *)malloc(30*sizeof(char));
    if (fp == NULL)
        printf("Error!!");
    printf("\nEnter your Reservations iD:\n");
    scanf("%d", &choice);
    fseek(fp, 0, 0);

    while (rec)
    {


        if (choice == temp->id) {
            fread(&temp->id, sizeof(int), 1, fp);
            printf("\niD: %d\n", temp->id);

            fread(temp->first_name, 10, 1, fp);
            printf("First Name: %s\n", temp->first_name);

            fread(temp->last_name, 15, 1, fp);
            printf("Last name: %s\n", temp->last_name);

            fread(temp->passport, 15, 1, fp);
            printf("Passport: %s\n", temp->passport);

            fread(temp->nationality, 30, 1, fp);
            printf("Nationality: %s\n", temp->nationality);

            fread(temp->room, 10, 1, fp);
            printf("Room: %s\n", temp->room);

            fread(&temp->bed, sizeof(int), 1, fp);
            printf("Beds: %d\n", temp->bed);

            fread(temp->email, 30, 1, fp);
            printf("Email: %s\n", temp->email);

            fread(&temp->phone_number, sizeof(int), 1, fp);
            printf("Phone number: %d\n\n\n", temp->phone_number);
        }
        rec--;
    }
    fclose(fp);
    free(temp);
    free(temp->first_name);
    free(temp->last_name);
    free(temp->passport);
    free(temp->room);
    free(temp->email);
    free(temp->nationality);
}

标签: c

解决方案


你的 count 和 rec 是未定义的,因为它们没有初始化。

在比较之前请先阅读 Id。

 fread(&temp->id, sizeof(int), 1, fp); 
  printf("\niD: %d\n", temp->id);
  if (choice == temp->id) {

}

推荐阅读