newbie needs help!!!!!

Posted on 2003-03-20
Medium Priority
Last Modified: 2010-04-15
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
Question by:helpmegenya
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 48

Accepted Solution

AlexFM earned 152 total points
ID: 8172296
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.

LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 148 total points
ID: 8172339
>> 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.

LVL 20

Expert Comment

ID: 10189605
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
LVL 30

Expert Comment

by:Mayank S
ID: 10206687
Please proceed with that recommendation.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question