0

Ive got a phonebook app that the user can enter in contact information, and it will show up in the phonebook. I have a delete function that allows the user to search via the last name and delete the contact from the phonebook. My issue is that when I search the last name to delete the contact, not only that contact, but also all the contacts that follow it.

void delete_contact(fr*friends ,int* counter, int i)
{
    char name_search[50]={'\0'};
    char Delete[5]={'\0'};

     printf("Search by last name\n");
     scanf("%s",name_search);
     for(i=0;i<*counter;i++)
      {
       if(strcmp(name_search,friends[i].Last_Name)==0)
       {
       strcpy(friends[i].Last_Name,Delete);
       (*counter)--;

      } 
    }                                        
 }

Now I realize that I will need to write a few more strcpy functions to overwrite the rest of the info within the contact connected to the last name, but right now I need to figure out why it's deleting all other names following in the phonebook. Ideas?

I can post more code or my output if needed. Thanks

DatDudeJC
  • 43
  • 1
  • 2
  • 9
  • Shouldn't you return, right after decrementing the counter? – imreal Nov 03 '12 at 05:13
  • Once the `if` condition matches, aren't you suppose to `break;` from the loop. Assuming there are no duplicate names. Why are you decrementing the counter anyway ? – Mahesh Nov 03 '12 at 05:14
  • If you are putting anything important on argument "i", it gets lost the for loop. – imreal Nov 03 '12 at 05:15
  • Is Last_Name just a fixed char buffer (i.e. char Last_Name[10] or something like it?) If possible, please include your `struct fr` definition also, since just blasting over a structure that has dynamic pointers with zeros is a sure-fire way to leak memory. – WhozCraig Nov 03 '12 at 05:40
  • @mahesh I solved the issue by using a flag, but I credit you for causing me to think about the `break` idea :)! If you wanna give that as an answer rather then a comment, I'll give you the rep points. – DatDudeJC Nov 03 '12 at 05:41
  • Want to clear whole record? `memset(&(friends[i]),'\0',sizeof(fr));` – weston Nov 03 '12 at 10:01
  • @DatDudeJC Glad you solved it :) – Mahesh Nov 03 '12 at 21:09

2 Answers2

0

What you're trying to do is slightly simple if you have all the "info" needed for deletion within struct fr.

Assuming its just a linked list, you will need to write a delete function that will remove individual nodes from the linked list

If its a simple array, you can overwrite the data with a bunch of "default" values(which can be filled the next time you search for storage space).

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

After your (*counter)-- get decremented you must exit from a loop via a 'break' statement.

suraj_fale
  • 978
  • 2
  • 21
  • 53