• 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");
        scanf("%d",&arr[test].age);

strcpy(arr[test].addr,addr);


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
0
helpmegenya
Asked:
helpmegenya
  • 2
2 Solutions
 
AlexFMCommented:
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:

1)
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++)

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

Looks strange, maybe this is better:

scanf("%d", &id);

3)

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.

0
 
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

else
  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.

Mayank.
0
 
jmcgOwnerCommented:
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].

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

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

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