Home > OS >  I'm trying to print this modified parallel array
I'm trying to print this modified parallel array

Time:01-08

I'm trying to print modified parallel arrays, these arrays are read from a file and I add another element to them from the keyboard, however, when the program prints the new arrays something goes wrong and I get 000000 as a new element for every array despite the fact that the size of all the arrays has been incremented and I tested it, the new spot is filled with the right new element. here is the code for the 2 functions. the function that adds elements:

void createNewAccount(char names[MAXSIZE][MAXSIZE], int stdID[MAXSIZE], float phones[MAXSIZE], int registrationYear[MAXSIZE])
{
    int tid, ty;
    float tp;
    char tn[10];
   if(size == MAXSIZE){
       printf("Max size has been reached\n");
   }
   printf("Please enter a student number\n");
   scanf("%d", &tid);
   for(int o=0; o<size; o  ){
       if(stdID[o]==tid){
           printf("The student already exists\n");
       }
   }
   size  ;
   stdID[size]=tid;
   printf("Please enter a phone number\n");
   scanf("%f", &tp);
   phones[size]=tp;
   printf("Please enter a registration year\n");
   scanf("%d", &ty);
   registrationYear[size]=ty;
   printf("Please enter a name\n");
   scanf("%s", tn);
   strcpy(names[size], tn);
}

and the function that prints is:
void viewList(char names[MAXSIZE][MAXSIZE], int stdID[MAXSIZE], float phones[MAXSIZE], int registrationYear[MAXSIZE])
{
  for(int j=0; j < size; j  ){
           printf("%s %0.2f %d %d\n", names[j], phones[j], stdID[j], registrationYear[j]);
        }
}

That problem is fixed now the problem is tommy 0.00 0 0 the data from the file is:

alex 599659008.00 19701112 2010
mark 599232832.00 19702315 2015
tommy 59965680.00 197012415 2020

and the result I'm getting is:

alex 599659008.00 19701112 2010
mark 599232832.00 19702315 2015
tommy 59965680.00 197012415 2020
tommy 0.00 0 0

CodePudding user response:

I suspect the problem is you are doing your size too early. If size starts at the number of entries you currently have, you are writing to the element following the one you intend to in createNewAccount(). Shift the size to the bottom of that function and things should work.

CodePudding user response:

All i had to do is to increase the size of tn from 10 to 50.

  •  Tags:  
  • Related