Thread: How to pass values to elements in array of structs?

  1. #1
    Registered User
    Join Date
    Mar 2022
    Posts
    1

    How to pass values to elements in array of structs?

    I have 2 structs in a "patient records" database program. I have a function for adding new patients that checks if there's an empty element in the patient array, and if so, goes to a function that finds the next patient number.

    When I use my 'viewing records' function, the new patient number is there, rather there's some random numbers. And my strings either aren't there or are a single character. I'm probably making some very obvious assignment errors here:


    Code:
    void addPatient(struct Patient patient[], int max) { 
    int i = 0, index = 0, vacancy = 0, nextNum; 
    for (i = 0; !vacancy || i < max; i++) { 
    if (patient[i].patientNumber == 0) { 
    vacancy = 1; index = i; 
    } 
    } if (vacancy) { 
    patient[index].patientNumber = nextPatientNumber(patient, max); 
    // is this right? ^^^^^^^^^ 
    inputPatient(patient + index); 
    printf("\n*** New patient record added ***\n"); 
       } else { 
    printf("\nERROR: Patient listing is FULL!\n"); 
      } return; 
    }
    
    void inputPatient(struct Patient* patient) { 
    char name[30] = { 0 }; 
    printf("Number: %05d", patient->patientNumber); // << works! 
    printf("\nName : "); 
    scanf("%[^\n]", name); 
    strcpy(patient->name, name); // << only copying 1 char 
    inputPhoneData(patient); 
    return; }
    
    
    void inputPhoneData(struct Phone* phone) // not the whole function {
     int select, i, valNum = 1; char phoneNum[11] = { 0 }; 
    strcpy(phone->description, "TBD"); // << doesnt work. why?? return; }
    Last edited by slove; 03-31-2022 at 06:17 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,642
    Why was your code indented by a mental patient?
    And you need to show the struct.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,128
    Quote Originally Posted by slove View Post
    I have 2 structs in a "patient records" database program. I have a function for adding new patients that checks if there's an empty element in the patient array, and if so, goes to a function that finds the next patient number.

    When I use my 'viewing records' function, the new patient number is there, rather there's some random numbers. And my strings either aren't there or are a single character. I'm probably making some very obvious assignment errors here:


    Code:
    void addPatient(struct Patient patient[], int max) { 
    int i = 0, index = 0, vacancy = 0, nextNum; 
    for (i = 0; !vacancy || i < max; i++) { 
    if (patient[i].patientNumber == 0) { 
    vacancy = 1; index = i; 
    } 
    } if (vacancy) { 
    patient[index].patientNumber = nextPatientNumber(patient, max); 
    // is this right? ^^^^^^^^^ 
    inputPatient(patient + index); 
    printf("\n*** New patient record added ***\n"); 
       } else { 
    printf("\nERROR: Patient listing is FULL!\n"); 
      } return; 
    }
    
    void inputPatient(struct Patient* patient) { 
    char name[30] = { 0 }; 
    printf("Number: %05d", patient->patientNumber); // << works! 
    printf("\nName : "); 
    scanf("%[^\n]", name); 
    strcpy(patient->name, name); // << only copying 1 char 
    inputPhoneData(patient); 
    return; }
    
    
    void inputPhoneData(struct Phone* phone) // not the whole function {
     int select, i, valNum = 1; char phoneNum[11] = { 0 }; 
    strcpy(phone->description, "TBD"); // << doesnt work. why?? return; }
    View this Wikipedia page on indentation styles. Choose one and be consistent. Many programmers editors and IDE's have options for automatically formatting your code, and conforming to a specific style.

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    I've looked past the code formatting...

    Have a look at inputPhoneData()'s parameter:

    Code:
    void inputPhoneData(struct Phone* phone)
    Now have a look at when you call it:

    Code:
        inputPhoneData(patient);
    Why do you have "patient" there?

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,642
    @hamster, Probably a typo in posting since that code wouldn't compile and yet problems like "only copying 1 char" are runtime problems.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Quote Originally Posted by john.c View Post
    @hamster, Probably a typo in posting since that code wouldn't compile and yet problems like "only copying 1 char" are runtime problems.
    True, bit if they did manage to get things to compile, then they don't have warnings enabled, and this will definitely be an issue.

    I always like to think the best of people unless proven otherwise - In this case I wouldn't be surprised if it is a student struggling to learn to code using a cell phone and an online C complier web page.

    Well, at least I hope it is!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding elements to array of structs
    By Marta in forum C Programming
    Replies: 5
    Last Post: 04-08-2015, 08:20 AM
  2. Array of structs(adding/deleting elements to a file)
    By boebae in forum C Programming
    Replies: 8
    Last Post: 09-22-2013, 02:10 PM
  3. changing the values of the array elements
    By yugidallin in forum C++ Programming
    Replies: 4
    Last Post: 01-01-2013, 07:27 AM
  4. How do I assign values to elements of a 2D array?
    By Karyumi in forum C Programming
    Replies: 9
    Last Post: 06-21-2012, 02:48 PM
  5. Array elements values not being assigned
    By Sardien in forum C Programming
    Replies: 4
    Last Post: 02-11-2002, 01:45 PM

Tags for this Thread