Thread: Storing User Input in a Linked List

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    1

    Storing User Input in a Linked List

    Hello, I'm try to making an address book and I'm stumped on how to store strings entered by the user into a node in the linked list.

    This is the struct I have defined.

    Code:
    struct node{
        
        char name[25];
        char address[100];
        char phone[25];
        
        struct node *next;
        
    };
    .
    .
    .
    Code:
    void append(){
    
    
        struct node *temp = (struct node*) malloc(sizeof(struct node)); 
        
        if(root == NULL){    // Empty list
            
            printf("Enter Name: ");
    
    
            fgets(temp->name,25,stdin);
            temp->name[strlen(temp->name)-1] = '\0';
        }
        
    }
    The issue is that the fgets() function is not called instead I return to the options menu I have created in main, and I'm not sure as to why that is happening. I don't want to use scanf because I expect users to enter strings white space i.e name: "John Doe", and I don't want them to enter a string larger than the buffer.

    Any help would be nice. Thank You.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Lemme guess, you called scanf to read the menu option.

    FAQ > Flush the input buffer - Cprogramming.com
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing user input into arrays
    By Morriok in forum C Programming
    Replies: 1
    Last Post: 05-14-2017, 12:23 PM
  2. Regarding storing items into a linked list.
    By jsuite in forum C Programming
    Replies: 10
    Last Post: 11-29-2012, 11:03 AM
  3. Storing user input into integer array
    By samii1017 in forum C Programming
    Replies: 2
    Last Post: 10-28-2012, 03:31 PM
  4. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  5. Help with linked list (and reading user input)
    By p1kn1c in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 12:43 AM

Tags for this Thread