0

The issue I have is that after entering a value to be used by the variable action, the program immediately ends without doing any of the cases inside the switch. Will post the other functions if needed, but otherwise, I will refrain because it's too long and it will flood the page. Can anyone tell me why this is happening?

Driver code and output are shown below:

int main()
{
    // Creation of empty list
    struct Node* head = NULL;
    bool print = false;
    int action, placeNum, numIndex;

    while (print == false){
        printf("LINKED LIST CREATOR \n");
        printf("1. Insert node at the beginning\n");
        printf("2. Insert node at the end\n");
        printf("3. Insert node at specified position\n");
        printf("4. Delete node at the beginning\n");
        printf("5. Delete node at the end\n");
        printf("6. Delete node at specified position\n");
        printf("7. Display the linked list\n");
        printf("Please enter the action you want to do: ");
        scanf("%d", action);
        
        switch (action)
        {
            case 1:
            printf("Enter the number to be placed at the beginning: ");
            scanf("%d", placeNum);
            insert_first(&head, placeNum);
            break;

            case 2:
            printf("Enter the number to be placed at the end: ");
            scanf("%d", placeNum);
            insert_last(&head, placeNum);
            break;

            case 3:
            printf("Enter the index for the number to be placed: ");
            scanf("%d", numIndex);
            printf("\nEnter the number to be placed at specified index: ");
            scanf("%d", placeNum);
            insert_middle(numIndex, placeNum, &head);
            break;

            case 4:
            delete_first(&head);
            break;

            case 5:
            delete_last(head);
            break;

            case 6:
            printf("Enter the index of the number to be deleted: ");
            scanf("%d", numIndex);
            delete_middle(&head, numIndex);
            break;

            case 7:
            printf("\nThe Created Linked List is: ");
            printList(head);
            print = true;
            break;
        }
    }

    return 0;
}

PS C:\Codes> cd "c:\Codes\C++\" ; if ($?) { g++ singlelist.cpp -o singlelist } ; if ($?) { .\singlelist }
LINKED LIST CREATOR 
1. Insert node at the beginning
2. Insert node at the end
3. Insert node at specified position
4. Delete node at the beginning
5. Delete node at the end
6. Delete node at specified position
7. Display the linked list
Please enter the action you want to do: 1
PS C:\Codes\C++> 

2 Answers2

2

You're not using scanf correctly:

scanf("%d", action);

The %d format specifier expects the address of an int (i.e. an int *) but you're instead passing an int, and an uninitialized one at that. Using the wrong argument type for a format specifier triggers undefined behavior.

If you pass the address:

scanf("%d", &action);

You'll get the result you expect. You'll want to do the same for you other scanf calls that use %d.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    That's a clear sign that Aldrin isn't enabling the a decent set of warnings when compiling - any decent compiler will check argument types for formatted I/O functions when the format string is a literal like that. – Toby Speight Dec 02 '22 at 15:49
  • The following question may be useful: [Why should I always enable compiler warnings?](https://stackoverflow.com/q/57842756/12149471) – Andreas Wenzel Dec 02 '22 at 16:09
0

scanf receives a format string and a POINTER to the variable where it will be stored.

So in this case the line should be: scanf("%d", &action);