Thread: Inserting into linked list

  1. #1
    Registered User
    Join Date
    Nov 2008

    Inserting into linked list

    I'm having a problem inserting a new node into a linked list that I've already made and sorted. The original list was made from a file, now the user has the option to add nodes to the list.

    Here's the code for insertion (where I think the problem lies), and more explanation of the problems I'm having follow:

        //Query user to add people
        cout << "Would you like to add a person to this list? yes / no" << endl;
        cin >> add_resp;
        while (add_resp == "yes")
              int keepgoing = 1;
              while (keepgoing == 1)
                    cout << "Enter the name for the person to be added as \"last_name, first_name\"" << endl;
                    getline(cin, name);
                    newNode = last;
                    newNode -> name = name;
                    cout << "Enter the ID number for " << name << endl;
                    getline(cin, ID);
                    newNode -> ID = ID;
                    cout << "Enter the address for " << name << endl;
                    getline(cin, address);
                    newNode -> address = address;
                    cout << "Would you like to add another person? yes / no" << endl;
                    getline(cin, add_resp);
                    if (add_resp == "no")
                       keepgoing = 0;
    The first problem is that when the prompt shows up asking to add a person, if you input yes, the user is then prompted for the name, immediately followed by prompt for the ID, with no chance to input the name. The user is allowed to input the ID, then prompted for address and inputs that, then is prompted about adding more.

    Why is this happening? Why is the user never given the opportunity to enter the name? When you respond to the second query about adding a person (after you've added one) you are able to input all info just fine.

    Secondly, the insertion overwrites one of my previous nodes. I have 6 from the text file, and throughout I only have six, one is continuously deleted (if I add more than 1 person, it overwrites one of the original nodes and keeps overwriting itself.).

    Now I know the last part is because I haven't inserted it correctly and set it to point to the next node, I've tried a few different ways and have been unsuccessful.

    How can I fix this!?

  2. #2
    Registered User
    Join Date
    Mar 2007
    When you use cin>> followed by getline() like you're doing, you need to use cin.ignore() after the cin. There is a return character left in the input buffer that the cin does not pick up, but getline does. getline() then picks up this hanging return and thinks you entered a line (without getting a chance to input). Also, you should use strcmp or strncmp instead of if(resp == "yes"), so if(strncmp(resp, "yes") == 0).

  3. #3
    Registered User
    Join Date
    Nov 2008
    Awesome, thanks for that.

    cin.ignore worked great, and I used ( string2) instead of strcmp (compiler didn't like strcmp), but thanks for the idea -- I was looking for a way to tidy up that bit a little as I thought two while loops like that should have a workaround.

    Anyone give me an idea about how to finish the insertion of the node into the linked list? That last bit will end my questions (until I get a new assignment and get lost again!)

  4. #4
    Registered User
    Join Date
    Nov 2008
    I figured it out.

    Thank you all for all of the help you've given!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  3. Inserting new nodes in a sorted double linked list
    By carrja99 in forum C++ Programming
    Replies: 2
    Last Post: 03-07-2003, 08:34 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM