View Single Post
  #1 (permalink)  
Old 03-27-2008, 07:08 PM
esmeco esmeco is offline
Newbie
 
Join Date: Mar 2008
Posts: 1
Rep Power: 0
esmeco is on a distinguished road
Default 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:

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!
Reply With Quote

Sponsored Links