0

This is part of a deletion function in a linked list program. I'm trying to compare the fields of the current struct node to fields read in by the user for searching. I know my node referencing works as I can print out current fields on their own, but my comparisons in the if statement to the variables does not. How can I compare the fields of current to the user data? Thanks

int deleteEmployee(void)
{
    char name[MAX_NAME_LENGTH];
    char gender[2];
    int age;
    char position[MAX_JOB_LENGTH];
    int placeInList = 0;

    Employee *current = employeeListHead;
    Employee *previous = employeeListHead;

    printf("Enter details of employee to delete: \n");
    printf("Name: \n");   
    scanf(" %100[^\n]s", name); 
    scanf("%*[^\n]%*c");

    printf("Gender: \n");
    scanf(" %1s", gender); 
    scanf("%*[^\n]%*c");

    printf("Age: \n");
    scanf(" %d", &age); 
    scanf("%*[^\n]%*c");

    printf("Title: \n");
    scanf(" %100[^\n]s", position); 
    scanf("%*[^\n]%*c");

    //while elements in list to search
    while(current != NULL)
    {   
    

This Specifically

        //handling a match on each iteration
        if (current->name == name && current->gender == gender && current->age == age && current->position == position)
        {
            printf("\nIs this the emplyee you'd like to delete? Please confirm (Y/N) %s %s %d %s \n\n", name, gender, age, position);
            char choice;
            scanf(" %c", &choice);

            //if delete is confirmed
            if (choice == 'Y' || choice == 'y')
            {   
                //if head of list
                if(current == employeeListHead)
                {
                    employeeListHead = current->next;
                    current = NULL;
                    return EXIT_SUCCESS;
                }
                //if tail
                else if(current->next == NULL)
                {   
                    //change previous nodes pointer
                    for (int i=0; current!=NULL && i < placeInList-1; i++)
                    {
                        previous->next = NULL;
                    }
                        
                    current = NULL;
                    
                    return EXIT_SUCCESS;
                }
                //if inside list
                else
                {
                    for (int i=0; current!=NULL && i < placeInList-1; i++)
                    {
                        previous->next = current->next;
                    }                    
                    current = NULL;
                    return EXIT_SUCCESS;
                }
                 
            }//end if yes selected to delete

            //if delete is confirmed
            if (choice == 'N' || choice == 'n')
            {
                printf("You selected N. Returning to main menu.");
                EXIT_FAILURE;
            }//end if no selected to delete

        }//end if a match

        //iterate to next set of nodes
        current = current->next;
        previous = previous->next;
        placeInList++;

    }//end iterating through list
    

    printf("Employee not found in system.\n");
    return EXIT_FAILURE;

    
}
CCAas
  • 1
  • 1

1 Answers1

0

You need to use strcmp to compare strings. When you write current->name == name, your program is just comparing the location of two pointers. (But you didn't show the definition of name, so it's hard to be sure.)

Try replacing all string comparisons with something like:

0 == strcmp(current->name, name)
David Grayson
  • 84,103
  • 24
  • 152
  • 189