0

我有一个模块可以搜索文件以查找索引,如果找到它应该打印与该索引相关的详细信息。程序编译运行,但不管病人是否被保存,都会打印出病人没有找到。我缺少什么逻辑错误?

注意:patientCount 是一个全局变量,它被写入另一个文件并在每次添加患者时更新。

void patientSearch(struct patientRec patient[], struct apptRec appt[])
{    
    system("cls");
    int c=0;
    char search[6], admit;

    printf("Enter the patient ID of the patient you would like to search for.\n");
    scanf("%i", &search);
    fflush(stdin);

    FILE *fp;

    fp=fopen("patients.txt", "r");
    if(fp==NULL)
    {
        printf("\nError opening file!");
    }
    else
    {

        for (c=0; c<patientCount; c++)
        {
            if (search==patient[c].patientID)
            {
                printf("\nPatient found.\n\n");
                printf("Patient ID: %i", patient[c].patientID);

                fscanf(fp, "%s", patient[c].fName);
                printf("\nPatient First Name: ");
                puts(patient[c].fName);

                fscanf(fp, "%s", patient[c].sName);
                printf("\nPatient Surname: ");
                puts(patient[c].sName);

                fscanf(fp, "%i %c", patient[c].age, patient[c].sex);
                printf("\nPatient Age: %i\nPatient Sex: %c", patient[c].age, patient[c].sex);

                fscanf(fp, "%s", patient[c].notes);
                printf("\n\nNotes: \n");
                puts(patient[c].notes);
            }
            else
            {
                fscanf(fp, "\n");
            }
        }
    }

    fclose(fp);
    if (c==patientCount)
    {
        printf("\nThis patient does not exist. Would you like to admit this patient?\n1: Yes\n2: No\n");
        scanf(" %c", &admit);
        if (admit=='1')
        {
            admitPatient(patient, appt);
        }
    }

}
4

1 回答 1

1
char search[6], admit;
scanf("%i", &search);
if (search==patient[c].patientID)

要么更改为

int search; // This allows the rest of the code to match

或更改为

char search[6], admit; //Change the rest of the code to match
scanf("%s", &search);
if (strcmp(search, patient[c].patientID) == 0)
printf("Patient ID: %s", patient[c].patientID);

以相同的格式进行输入和比较。

确保您的搜索数组足够大以包含最后的 '\0'

于 2016-02-23T22:15:45.233 回答