• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

newbie needs help!!!!!

void modify_rec(my_rec_t arr[], int size){
     int id;
     int test=0;
     int i;
     char c;
     char addr[40];
     printf("Enter id of the record to be modified:\n");
     scanf("%d%*c", &id);

    for (i = 1; i < size+1; i++)
        // next: compare names (hint: use a library function)
        if (arr[i].id == id)
            test = i+1;

          if (test==0){
printf("Record does not exist.\n");
return size;



     printf("Enter Address:\n");
      fgets(addr, 40, stdin);
      addr[strlen(addr)-1] = '\0';
      printf("Enter Age:\n");


right now, 2 cases...either the program will ask you for an address regardless of if there is a id that exists within arr[100] or it will continously say record does not exist
  • 2
2 Solutions
return size;

Executing of this line means that the code in the end of function is not executed. Program doesn't ask for address if record is found.

There are some other problems in this code:

for (i = 1; i < size+1; i++)
    if (arr[i].id == id)

Arrays in C are null-based, so I guess this line should be:

for (i = 0; i < size; i++)

scanf("%d%*c", &id);

Looks strange, maybe this is better:

scanf("%d", &id);


if (test==0){
printf("Record does not exist.\n");
return size;

I think these lines should be after the loop for{...}, otherwize your function returns on the first array element.

Mayank SAssociate Director - Product EngineeringCommented:
>> return size;

The return-type specified is a 'void'. It cannot return any value.

>> scanf("%d%*c", &id);

Ok, this will read only an integer. Probably you were about to read a character after the integer but then decided to omit the character.

>> for (i = 1; i < size+1; i++)
>>   {
>>       // next: compare names (hint: use a library function)
>>       if (arr[i].id == id)
>>       {
>>           test = i+1;

>>         if (test==0){
>> printf("Record does not exist.\n");
>> return size;
>>         }

Why to compare names? If you have an ID for identifying every record, then you don't need to compare the names. And the second 'if' block should be outside the loop. And remember that in C, array-indexing starts at 0. Also, since 'id' is a member of the structure, I'd prefer to use some other variable for the local variable 'id' in the function:

How about:

int test = 0 ;
int num ;
char str[41] ;

printf ( "\n Enter the ID: " ) ;
scanf ( "%d", &num ) ;

for ( i = 0 ; i < size ; i ++ )
  if ( arr[i].id == num )
    test = 1 ;
    break ;

  } // end if, for

if ( test == 0 )
  printf ( "\n Not found! " ) ;
  arr[size].id = num ;
  printf ( "\n Enter address: " ) ;
  fflush ( stdin ) ;
  gets ( str ) ;
  strcpy ( arr[size].name, str ) ;
  printf ( "\n Enter age: " ) ;
  scanf ( "%d", &arr[size].age ) ;

} // end if

  printf ( "\n Found! " ) ; // end else

In this example, I have assumed that 'size' is the number of elements in the array and that the relevant array indices are 0 through size - 1. You will also have to increment the value of size after adding this new record, but since 'size' is a local variable, this change in 'size' will not be reflected in the calling function. I suggest that you pass the size by address and use a pointer argument in the function.

If you need moe help, then please post a comment.

Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points between AlexFM [25 pts] and mayankeagle [50 pts].


EE Cleanup Volunteer
Mayank SAssociate Director - Product EngineeringCommented:
Please proceed with that recommendation.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now