Array of structures problems

This is a discussion on Array of structures problems within the C Programming forums, part of the General Programming Boards category; Hello! I'm currently working on a project and I'm having a bit of an hard time with two functions in ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    Array of structures problems

    Hello!

    I'm currently working on a project and I'm having a bit of an hard time with two functions in specific:

    Function 1:In this function I want the doctor's number of patients to increase after patient has consulted that doctor.
    As so,by means of testing the function,in the menu, I add a doctor to the array of doctors and then I add a patient.In the patient fields that are asked in program(name,age,address), I fill the field name of the doctor with the doctor that
    treated/will treat him.
    After that,I check the doctor's data and the number of patients has indeed increased by one.

    Problem:


    If I add another doctor and add another patient,and in the patient's doctor name field I add the new doctor name,in the doctor's data,instead of just incrmenting the new doctor's patient it increments the new doctor's patient and the other doctor!

    Code:
    void addpatients_to_doctors(Doctors array_of_doctors[],Patients array_of_patients[],int ndoctors,int npatients)
    {
    	int i,z,count=0,x;
    
    	for(i=0;i<ndoctors;i++)      // Goes through array of doctors
    	  {
    	     for(x=0;x<npatients;x++) // Goes through array of patients
    	       {
    	               for(z=0;z<array_of_patients[x].nconsultations;z++)  // Goes through number of existing consultations
    	                {
    		            if(strcmp(array_of_doctors[i].p.name, array_of_patients[x].h[z].doctorname)==0)  //compares the name in the doctor's file with the name of the doctor in the patient's historic consultations 
    			      count++;	  
    	                }
    	             if(count==1)
    		        array_of_doctors[i].npatients++;
    
    		     count=0;
    	       }
    	  }
    }

    Function 2:In this function I want the patient's historic to have at a maximum 10 consultations from the oldest in the beggining of the array to the newest. After reaching the 10 consultations, the consultation on pos 1 of array will be copied to pos 0,the consultation from pos 2 to pos 1 and from that forward until it reach the last pos where it will have "today's" consultation.

    Problem:

    This all works alright after I reach the 10 consultations.After that it looks like all the historic is erased and it copies the newwest consultation to teh beggining having only that input on the historic.

    Code:
    int keep_data_con(Patients array_of_patients[],Doctors array_of_doctors[],int npatients)
      {
    	  int i,z,nconsultations;
    	  char nid[12];
    	  
    
         printf("Input your id:\n");
    	 gets(nid);
    	 for(i=0;i<npatients && strcmp(nid,array_of_patients[i].p.npid)!=0; i++);
    
         if(i==n)
          {
            printf("Patient doesn't exist\n");
            return 0;
    	  }
    	 else
    	 {
    	  nconsultations=array_of_patients[i].numconsultations++;
    	  if(nconsultations==10)
    	    {
    
    		  for(z=0;z<nconsultations;z++)
    			array_of_patients[i].h[z]=array_of_patients[i].h[z+1];	
    
           array_of_patients[i].h[numconsultations-1]=get_data_conultations(array_of_doctors,npatients);
    	    }
    	  else
            array_of_patients[i].h[numc]=get_data_consultations(array_of_doctors,npatients);
    	 }
    }

    Any help on this is very appreciated!Thanks in advance!
    Last edited by esmeco; 03-27-2008 at 04:25 PM. Reason: horrible code format

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    do not use gets - read FAQ

    indent your code

    and because you failed to give some readable names to your criptic vars - add comments along your code explaining what is happening in each line. If you cannot understand what is happening in your code - how you suppose someone else could undestand it with such horrible coding style?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Maybe for Problem 1, instead of passing in an array_of_patients, you could just pass in a struct for the new patient:
    Code:
    void addpatients_to_doctors(Doctors array_of_doctors[], Patients patient, int ndoctors, int npatients)
    And then the check would be:
    Code:
    		            if(strcmp(array_of_doctors[i].p.name, patient.h[z].doctorname)==0)  //compares the name in the doctor's file with the name of the doctor in the patient's historic consultations 
    			      count++;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of structures
    By tish in forum C Programming
    Replies: 9
    Last Post: 04-05-2009, 03:17 AM
  2. Dictionary into a 2d Char Array... Problems.
    By Muzzaro in forum C Programming
    Replies: 10
    Last Post: 12-02-2006, 11:34 AM
  3. trying to initialize an array of structures
    By dreamgoat in forum C Programming
    Replies: 4
    Last Post: 09-26-2004, 05:33 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21