0

So I have a linkedlist and I am trying to delete the name of the input, but the delete function cannot find the name on the list. I am trying to find the logic flaw, I need some help!

some knowledge to know: name is a character array in the struct

 case 4:
                     if(head == NULL)
                        printf("List is Empty\n");
                    else
                    {
                        printf("Enter the name to delete : ");
                        scanf("%s",&user);

                        if(delete(user))
                            printf("%s deleted successfully\n",user);
                        else
                            printf("%s not found in the list\n",user);
                    }
                    break;




int delete(const char* input)
{
    struct person *temp, *prev;
    temp = head;

    while(temp != NULL)
    {
         if(temp->name == input)
        {
            if(temp == head)
            {
                head = temp->next;
                free(temp);
                return 1;
            }
            else
            {
                prev->next = temp->next;
                free(temp);
                return 1;
            }
        }
        else
        {
            prev = temp;
            temp = temp->next;
        }
    }
    return 0;
}
codegeek123
  • 43
  • 1
  • 5
  • what are you trying to do here? temp->name == input You are comparing pointer. – Julien Leray Jun 02 '15 at 01:57
  • Read this: ["How do I check if a value matches a string?"](https://stackoverflow.com/questions/1598425/how-do-i-check-if-a-value-matches-a-string) And fwiw, this can be [considerably shortened](http://pastebin.com/5YTxYDL0). – WhozCraig Jun 02 '15 at 03:21

2 Answers2

1

if(temp->name == input) is just comparing pointers. Try if (strcmp(temp->name, input) == 0)...

John3136
  • 28,809
  • 4
  • 51
  • 69
1

Donot use temp->name == input, use strcmp(temp->name, input) see strcmp

blindpirate
  • 114
  • 1
  • 5