Lester Crest
Lester Crest

Reputation: 21

How to add keep asking for user input and save all input to file

I have a certain problem with my code: Whenever I want to add a new patient, something fishy happens. If I write one patient at a time and then quit the registration, the patient I have typed in will be found in the file. But if I write more than one patient at a time, it's only the last patient input that will be saved and the rest isn't saved. I believe it has something to do with my array, but I can't put my finger on it. Any advice?

Here is the code:

    FILE *patientfile;
    char answer;

    patientfile= fopen("test.txt", "a");
    if (!patientfile)
        {
        printf(" Failed to open file.\n");
        }

    do
    {
        int i=0;

        printf(" Enter details of patient %d\n\n", i+1);
    
        printf(" Patients first name: ");
            scanf("%s",arr_patient[i].fname);
        printf(" Last name: ");
            scanf("%s",arr_patient[i].lname);
        printf(" Personnummer: ");
            scanf("%d", &arr_patient[i].birthdate);
        printf(" Bildref: ");
            scanf("%d", &arr_patient[i].bildref);
        
        printf(" Do you want to add someone else?: (y/n):");
            scanf(" %c",&answer);
    
        i++;
    
    }while(answer != 'n');

    int i = 0;

    fprintf(patientfile," %s\t%s \n ", arr_patient[i].fnamn,arr_patient[i].lnamn);
    fprintf(patientfile," %d \n",arr_patient[i].birthdate);
    fprintf(patientfile," %d \n",arr_patient[i].bildref);



    fclose(patientfile);

    }

EDIT: Problem fixed! Thanks everyone!

Upvotes: 1

Views: 72

Answers (2)

4386427
4386427

Reputation: 44274

I see two problems:

You are setting i to zero in every loop:

do
{
    int i=0;

so all reading goes to array index zero. Move the initialization out of the loop:

int i=0;
do
{

so that the first record will be stored at array index 0, the second record at array index 1 and so on.

Further, you need a loop when writing to the file. Currently you only write one record. To write all records do something like:

for (int j = 0; j<i; ++j)
{
    fprintf(patientfile," %s\t%s \n ", arr_patient[j].fnamn,arr_patient[j].lnamn);
    fprintf(patientfile," %d \n",arr_patient[j].birthdate);
    fprintf(patientfile," %d \n",arr_patient[j].bildref);
}

Upvotes: 0

Ameen
Ameen

Reputation: 1857

This is how it should be done (I embedded notes about the problems in the code)

char answer;
// NOTE: this must be outside the loop
int i=0;
do
{
    printf(" Enter details of patient %d\n\n", i+1);

    printf(" Patients first name: ");
    scanf("%s",arr_patient[i].fname);
    printf(" Last name: ");
    scanf("%s",arr_patient[i].lname);
    printf(" Personnummer: ");
    scanf("%d", &arr_patient[i].birthdate);
    printf(" Bildref: ");
    scanf("%d", &arr_patient[i].bildref);

    printf(" Do you want to add someone else?: (y/n):");
        scanf(" %c",&answer);

    i++;

}while(answer != 'n');

FILE *patientfile = fopen("test.txt", "a");
if (!patientfile) printf(" Failed to open file.\n");

// NOTE: You need to loop over the array and write all the patients to the file
for(int j=0; j<i; j++)
{
    fprintf(patientfile," %s\t%s \n ", arr_patient[j].fnamn,arr_patient[j].lnamn);
    fprintf(patientfile," %d \n",arr_patient[j].birthdate);
    fprintf(patientfile," %d \n",arr_patient[j].bildref);
}
fclose(patientfile);

But you don't actually need the array. You can write the patients to the file directly like this:

FILE *patientfile = fopen("test.txt", "a");
if (!patientfile) printf(" Failed to open file.\n");
char answer;
int i=0;
do
{
    // change patient_struct to your structure name
    patient_struct patient;
    printf(" Enter details of patient %d\n\n", i+1);

    printf(" Patients first name: ");
    scanf("%s",patient.fname);
    printf(" Last name: ");
    scanf("%s",patient.lname);
    printf(" Personnummer: ");
    scanf("%d", &patient.birthdate);
    printf(" Bildref: ");
    scanf("%d", &patient.bildref);

    fprintf(patientfile," %s\t%s \n ", patient.fnamn, patient.lnamn);
    fprintf(patientfile," %d \n", patient.birthdate);
    fprintf(patientfile," %d \n", patient.bildref);

    printf(" Do you want to add someone else?: (y/n):");
        scanf(" %c",&answer);

    i++;

}while(answer != 'n');
fclose(patientfile);

Upvotes: 1

Related Questions