Thread: How to store Patient names to a number?

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    7

    Red face How to store Patient names to a number?

    Hi all,

    I am trying to write a code where i store patient name and a patient number. (i will be entering multiple names)
    further on through the program i want to enter that number and it will display the name.

    Here is what i have so far.

    Code:
    while(1)
       {
          printf("\nSelect one of the following numbers and press enter"); //Alerts user to selsct the following command. 
          printf("\n1.Patient Details");   //Assume we will have maximum 15 patients
          printf("\n2.Allocate Patient to Doctor");  //prompts user to allocate dcotors to patients: Jerome Mcclain, Teresita Lieu, Rocky Michael (Doctors names)
          printf("\n3.Call Patient");   //Call the nextpatient and send them to see their Doctor
          printf("\n4.Report");         //Generates a clinic financial report
          printf("\n----------------"); //prints newline
          scanf("%d",&choice); //Stores users choice 
       
        switch (choice)
        {
        case 1:
        {
        
          printf ("\nEnter the following and press enter"); //Prompts user to enter the following details followed by enter key
          printf ("\nFirst Name and last name"); //First Name Last Name, max 25characters, show full name to user  
          scanf ("%s %s",patientname[i].Firstname,patientname[i].Lastname); // First name stored and last name stored
          printf ("\nFull name:%s %s", patientname[i].Firstname,patientname[i].Lastname);    //Full name to be shown    
          printf ("\nMedicare Card Number:"); //5digits entered, show digits to user, Receive the digits as a string of 5characters 
          scanf("%s",patientname[i].cardnumber); // Medicare card number stored 
          printf("%s\n",patientname[i].cardnumber); // Medicare number shown 
          printf ("Symptoms:");  //Comma separated list of symptoms, max 25 characters, show symptoms to user
          scanf ("%s", patientname[i].symptoms); // Symtoms for pateient stored 
          printf ("%s\n", patientname[i].symptoms);// Symtoms shown on screen
          printf ("Patient Number:"); //Generate patient number and display it this number should be an integer equal to the number of patients currently in the system + 1
          scanf ("%s",patientname[i].patientnumber); // Patient number stored 
          printf ("%s\n",patientname[i].patientnumber); // Patient number shown
          
        
        }   
      break;
      
      case 2:
      {
        printf ("\nSelect Doctor:\n For Jerome McClain connect Pin 8.\n For Teresita Lieu connect Pin 9.\n For Rocky Michael connect Pin 10."); // Tells user which pin is which for each doctor
       if (~PINH & (1<<PH5))    //If Pin 8 is selcted 
          {
            printf ("\nJerome McClain"); //Prints doctors name for pin 8 
            printf ("\nAssign Patient number to doctor:"); // Tells user to allcoate a patient to a doctor by means of there number
            scanf ("%s",patientname[i].patientnumber); // Adds Patient number to doctor
            printf ("%s",patientname[i].patientnumber); // Prints Patient number to screen 
            printf ("\nFull name:%s %s", patientname[i].Firstname, patientname[i].Lastname); //Prints patient name to screen
          }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,497
    Before you dig yourself into a large hole with a massively bloated main, add some structure to your code.

    1. Use lots of functions.
    For example, each of your top-level menu choices should map to functions which perform those tasks.

    2. Don't add trivial detail too early.
    For example, you'll be able to make the whole program work with just
    Code:
    struct Patient {
      char  firstName[FNAME_LEN];
      int   id;
    };
    It will make your testing go a lot quicker because you won't have to complete the full Q&A every time you run the program.


    Mini example.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    // The limit constants, and defined by your assignment
    #define MAX_PATIENTS  15
    #define MAX_DOCTORS    3
    #define FNAME_LEN     25
    
    struct Patient {
      char  firstName[FNAME_LEN];
      int   id;
    };
    
    struct Doctor {
      char  name[FNAME_LEN];
      int   numPatients;
      int   patientIds[MAX_PATIENTS];
    };
    
    void addPatient(struct Patient *patient, int givenID) {
      printf("Enter firstName\n");
      scanf("%s",patient->firstName);
      patient->id = givenID;
    }
    
    void allocatePatientToDoctor(struct Patient *patient, struct Doctor *doctor) {
      doctor->patientIds[doctor->numPatients] = patient->id;
      doctor->numPatients++;
    }
    
    void menu() {
      // By convention, \n goes at the end
      printf("Select one of the following numbers and press enter\n"); //Alerts user to selsct the following command.
      printf("1.Patient Details\n");   //Assume we will have maximum 15 patients
      printf("2.Allocate Patient to Doctor\n");  //prompts user to allocate dcotors to patients
      printf("3.Call Patient\n");   //Call the nextpatient and send them to see their Doctor
      printf("4.Report\n");         //Generates a clinic financial report
      printf("----------------\n"); //prints newline
    }
    
    int main ( ) {
      struct Patient patients[MAX_PATIENTS] = { 0 };
      struct Doctor  doctors[MAX_DOCTORS] = {
        { "Jerome Mcclain" },
        { "Teresita Lieu" },
        { "Rocky Michael" },
      };
      int numPatients = 0;
      int uniquePatientId = 1;
      int choice;
      while ( 1 ) {
        menu();
        scanf("%d",&choice); //Stores users choice
        switch ( choice ) {
          case 1:
            addPatient(&patients[numPatients],uniquePatientId);
            numPatients++;
            uniquePatientId++;
            break;
          case 2:
            // prompts for doctor and patient IDs, replace subscripts as appropriate.
            allocatePatientToDoctor(&patients[0],&doctors[0]);
            break;
        }
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2019
    Posts
    7
    Quote Originally Posted by Salem View Post
    Before you dig yourself into a large hole with a massively bloated main, add some structure to your code.

    1. Use lots of functions.
    For example, each of your top-level menu choices should map to functions which perform those tasks.

    2. Don't add trivial detail too early.
    For example, you'll be able to make the whole program work with just
    Code:
    struct Patient {
      char  firstName[FNAME_LEN];
      int   id;
    };
    It will make your testing go a lot quicker because you won't have to complete the full Q&A every time you run the program.


    Mini example.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    // The limit constants, and defined by your assignment
    #define MAX_PATIENTS  15
    #define MAX_DOCTORS    3
    #define FNAME_LEN     25
    
    struct Patient {
      char  firstName[FNAME_LEN];
      int   id;
    };
    
    struct Doctor {
      char  name[FNAME_LEN];
      int   numPatients;
      int   patientIds[MAX_PATIENTS];
    };
    
    void addPatient(struct Patient *patient, int givenID) {
      printf("Enter firstName\n");
      scanf("%s",patient->firstName);
      patient->id = givenID;
    }
    
    void allocatePatientToDoctor(struct Patient *patient, struct Doctor *doctor) {
      doctor->patientIds[doctor->numPatients] = patient->id;
      doctor->numPatients++;
    }
    
    void menu() {
      // By convention, \n goes at the end
      printf("Select one of the following numbers and press enter\n"); //Alerts user to selsct the following command.
      printf("1.Patient Details\n");   //Assume we will have maximum 15 patients
      printf("2.Allocate Patient to Doctor\n");  //prompts user to allocate dcotors to patients
      printf("3.Call Patient\n");   //Call the nextpatient and send them to see their Doctor
      printf("4.Report\n");         //Generates a clinic financial report
      printf("----------------\n"); //prints newline
    }
    
    int main ( ) {
      struct Patient patients[MAX_PATIENTS] = { 0 };
      struct Doctor  doctors[MAX_DOCTORS] = {
        { "Jerome Mcclain" },
        { "Teresita Lieu" },
        { "Rocky Michael" },
      };
      int numPatients = 0;
      int uniquePatientId = 1;
      int choice;
      while ( 1 ) {
        menu();
        scanf("%d",&choice); //Stores users choice
        switch ( choice ) {
          case 1:
            addPatient(&patients[numPatients],uniquePatientId);
            numPatients++;
            uniquePatientId++;
            break;
          case 2:
            // prompts for doctor and patient IDs, replace subscripts as appropriate.
            allocatePatientToDoctor(&patients[0],&doctors[0]);
            break;
        }
      }
    }

    Thanks Salem,

    I have now made the code more neat with your help.

    But, i still cant work out how to allocate a patient to a doctor, refer to the following part
    Code:
    case2:        // prompts for doctor and patient IDs, replace subscripts as appropriate.
            allocatePatientToDoctor(&patients[0],&doctors[0]);
            break;
    where 0 is in place what can i put for each number to be shown.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,497
    > printf("2.Allocate Patient to Doctor\n"); //prompts user to allocate dcotors to patients
    What does this actually mean?

    Which patient, and which doctor are we talking about here?

    You need to be prompting the user for both these bits of information.

    Say
    - print all the patients
    - print all the doctors
    - scanf("%d",&p);
    - scanf("%d",&d);

    Then you might be able to do
    allocatePatientToDoctor(&patients[p],&doctors[d]);


    How is your User Interface(UI) supposed to work?

    Do users always enter
    - choices 1,2,1,2 for 15 iterations,
    - then choice 3 for some number of iterations
    - and then a final choice 4 ?

    Or is it more free flowing like
    - choice 1,1,1,1 to input all the patients,
    - then choice 2,2,2,2 to assign all the patients,
    - then perhaps some sequence of 3,3,4,3,3,4 choices.

    If you always alternate 1,2, then you can maintain the idea of 'current patient' and that may simplify step 2 to only deciding which doctor to assign the patient to.

    > printf("3.Call Patient\n"); //Call the nextpatient and send them to see their Doctor
    Again, you also need to think about what your user inputs
    Do you input a doctor, and send their next patient to them.
    -- could be easier, because Doctors currently know about patientIds
    Do you input a patient, and find out which doctor they're supposed to see?
    -- could be harder, because Patients currently DON'T know about doctorIds

    How you decide to answer this will guide you as to how you end up implementing allocatePatientToDoctor.


    These are all design choices.
    Everything is possible, but the choices you make will make some things easier and at the same time make other things harder.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2019
    Posts
    7
    Thanks for your help,

    The user interface can select any options at anytime.

    for example they may select 1,1,1,2,2,3,1,1,2,2,3,4

    Most likely 4 would be the last selection to calculate all the patients seen by each doctor.

    The trouble i am having is when i get to allocating a patient to a doctor.

    For example i input details of 3 patients and i want to allocate patient 2 to doctor 1 by entering the patient id 2?

    I hope this makes some sense.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,497
    OK, so you do this
    - print all the patients
    - print all the doctors
    - scanf("%d",&p);
    - scanf("%d",&d);

    Then you might be able to do
    allocatePatientToDoctor(&patients[p],&doctors[d]);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Aug 2019
    Posts
    7
    Yes, that's the part i am struggling with, i know i can use an array to store the patients but how do i add the number and patient to the certain array using
    - print all the patients
    - print all the doctors
    - scanf("%d",&p);
    - scanf("%d",&d);

    I am confused on how to do that.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,497
    Are you telling me that given an an array like
    Code:
    struct Patient {
      char  firstName[FNAME_LEN];
      int   id;
    };
    struct Patient patients[MAX_PATIENTS] = { 0 };
    That you have NO idea how to write a for loop to print out each firstName from each element of the patients array?

    All of a sudden, you seem way out of your depth.

    I'm not doing your homework for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-08-2019, 02:57 AM
  2. How to store 15 digit number in C++
    By RITESHKAKKAR in forum C++ Programming
    Replies: 1
    Last Post: 11-06-2017, 04:34 AM
  3. Replies: 8
    Last Post: 12-09-2013, 10:27 PM
  4. Replies: 10
    Last Post: 10-14-2013, 11:56 AM
  5. Replies: 8
    Last Post: 02-22-2009, 05:17 PM

Tags for this Thread