-3

Case study 8 – Develop a Hospital Management System Write C programs to

  1. Store the patient details in to a text file.
  2. View the details of patients using phone number, NIC , etc…
  3. Store the patient channeling details to another text file.
  4. Display the summary of the patients. create 4 seperate c programs covering all sections.

Can someone please help me because I don't get an output for section 2 and 4. section 1 and 3 works fine. All for sections are interconnected I guess.

**This is the code I created for section 1:

#include<stdio.h>
int main(void)
{
    int pnum,age,cnum,nic;
    char name[20],gender[6],address[50];

    FILE * fp;
    fp = fopen("patient.txt", "a");

    printf("Add patient details\n\n");

                printf("Patient number: ");
                scanf("%d", &pnum);
                printf("First name: ");
                scanf(" %s", &name);
                printf("Gender: ");
                scanf(" %s", &gender);
                printf("Age: ");
                scanf("%d", &age);
                printf("Address: ");
                scanf(" %s", &address);
                printf("NIC: ");
                scanf("%d", &nic);
                printf("Contact number: ");
                scanf("%d", &cnum);

                fprintf(fp, "%d %s %s %d %s %d %d\n", pnum, name, gender, age, address, nic, cnum);


    fclose(fp);
    return 0;
}

**This is the code I created for section 2:

#include<stdio.h>
int main(void)
{
    int pnum,age,cnum,nic;
    char name[20],gender[6],address[50];

    FILE * fp;
    fp = fopen("patient.txt", "r");

    if ( fp != NULL)
    {
        while(!feof(fp))
        {
            fscanf(fp, "%d %s %s %d %s %d %d", pnum, name, gender, age, address, nic, cnum);
            printf("%d %s %s %d %s %d %d", pnum, name, gender, age, address, nic, cnum);
        }
        fclose(fp);
    }
    else
    {
        printf("could not open file\n");
    }
    return 0;
}

**This is the code for section 3:

#include <stdio.h> 
int main(void)
{
    int pnum,chnum,nic;
    char name[20],doc[20],sickness[50];

    FILE * fp1;
    fp1 = fopen("channeling.txt", "a");

    printf("Add patient channeling details\n\n");
    printf("Patient number: ");
    scanf("%d", &pnum);
    printf("Channeling number: ");
    scanf("%d", &chnum);
    printf("First name: ");
    scanf(" %s", &name);
    printf("NIC: ");
    scanf("%d", &nic);
    printf("Sickness: ");
    scanf(" %s", &sickness);
    printf("Prescribed doctor: ");
    scanf(" %s", &doc);


                fprintf(fp1, " %d %d %s %d %s %s\n", pnum, chnum, name, nic, sickness, doc);


    fclose(fp1);
    return 0;
}

**This is the code for section 4:

#include<stdio.h>
int main(void)
{
    int pnum,age,cnum,nic;
    char name[20],gender[6],address[50];

    FILE * fp;
    fp = fopen("patient.txt", "r");

    if ( fp != NULL)
    {
        while(!feof(fp))
        {
            fscanf(fp, "%d %s %s %d %s %d %d", pnum, name, gender, age, address, nic, cnum);
            printf("%d %s %s %d %s %d %d", pnum, name, gender, age, address, nic, cnum);
        }
        fclose(fp);
    }
    else
    {
        printf("could not open file\n");
    }
    return 0;
}
Paul T.
  • 4,703
  • 11
  • 25
  • 29
  • 1
    If you're compiler isn't giving you warnings that point out the obvious problem in your code, either it's a terrible compiler or you don't know how to get it to give you warnings. Think about this: In section 4, when you pass `pnum` to `fscanf`, what *value* are you passing to `fscanf`? C is purely pass-by-value for integers. So when you pass an integer parameter to `fscanf`, you pass that parameter's *value*. – David Schwartz May 06 '20 at 03:46
  • `while (!feof(fp))` is a bad idea – user12986714 May 06 '20 at 03:48

1 Answers1

0

Problems are:

  1. while (!feof(fp) feof indicates an error has occurred. So the logic in your program is not correct. (if fscanf does get an error, printf will still get executed)

  2. Forgot & in fscanf.

user12986714
  • 741
  • 2
  • 8
  • 19
  • I got an output for section 4 after adjusting. This works but it only reads the first line from the file. #include int main(void) { int pnum,age,cnum,nic; char name[20],gender[6],address[50]; FILE * fp; fp = fopen("patient.txt", "r++"); if ( fp != NULL) { fscanf(fp, "%d %s %s %d %s %d %d", &pnum, &name, &gender, &age, &address, &nic, &cnum); printf("%d %s %s %d %s %d %d", pnum, name, gender, age, address, nic, cnum); fclose(fp); } else { printf("could not open file\n"); } return 0; } – Kavindu Anjana May 06 '20 at 04:48
  • How do I use a while loop in this piece of code. I tried using while(!feof(fp)) but the output becomes infinite. it doesnt stop – Kavindu Anjana May 06 '20 at 04:51
  • @KavinduAnjana https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong?r=SearchResults – user12986714 May 06 '20 at 04:58