Trouble with linked list

This is a discussion on Trouble with linked list within the C++ Programming forums, part of the General Programming Boards category; *Edit* Ignore this portion, current issue is at post 5. I'm attempting to extract info from a text file and ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    54

    Trouble with linked list

    *Edit* Ignore this portion, current issue is at post 5.



    I'm attempting to extract info from a text file and load it into a linked list. The problem I'm running into is that I'm trying to only have 3 members for the struct, but when I try to read in the name, I'm having to split it up due to a white space in between.

    The text file is formatted as such:

    Last_name, First_name
    ID_Number
    Address

    How do I get the program to read in "Doe, John" as name. I want it to store the information just like it's read, i.e. I don't want it to remove the white space, rather just continue reading until it reaches a new line.

    I'll also have the same problem when it gets to address.

    Also, when I get there, I'm going to want to be able to search within the linked list by a particular member, say, name. But I'm going to only want to search using either first_name or last_name. I assume there will be a way for me to just search through the member type, at which point I'll have to tell it to stop searching when it reaches ',' -- yes?

    Here's what I've got so far:

    Code:
    struct node
    {
         char name[30];
         char lastName[30];
         int ID;
         char address[50];
         node *next;
    };
    
    int main(int argc, char *argv[])
    {
        ifstream inFile ("A1.txt");
        
        node *root;
        root = new node;
        root->next = 0;
        
        inFile >> root->name;
        inFile >> root->lastName;
        inFile >> root->ID;
        inFile >> root->address;
        
        cout << root->name     << endl
             << root->lastName << endl
             << root->ID       << endl
             << root->address  << endl;
    
    
        
        inFile.close();
    Last edited by mikeman; 01-21-2010 at 02:40 PM. Reason: updating

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by mikeman View Post
    How do I get the program to read in "Doe, John" as name. I want it to store the information just like it's read, i.e. I don't want it to remove the white space, rather just continue reading until it reaches a new line.
    Code:
    #include <string>
    #include <fstream>
    
    ifstream file;
    file.open(filename);
    string charline;
    
    getline(file, charline);
    You can search through the string until you find the ',' character, then discard it and skip the space. That can separate the first and last names, just like you said.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    I've tried using getline a few different times, and ran into some errors. I'd prefer to stick with the syntax I was using in the code I posted, does anyone know of a way to accomplish this?

    I edited my file and played around trying to use getline again after your post, and compiles but seems to hangup somewhere, when the output screen comes up it immediately closes and I get a windows error.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    Got it working. Will probably post back when I run into my next error along the way... ) thanks

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    I've got the linked list working (it seems at least). Now I want to query the user to add another node to the list.

    Here's the linked list so far:
    Code:
    struct node
    {
         string name;
         string ID;
         string address;
         node *next;
    };
    
    
    int main(int argc, char *argv[])
    {
        string line;
        char add_resp[3];
        ifstream inFile ("A1.txt");
        node *head = NULL;
        
        for (int i = 0; i < 20; i++)
        {
        
            node *newPerson;
            node *person_ptr;
        
            
            newPerson = new node;
        
            //Get name
            getline(inFile, line);
            newPerson->name = line;
            
            //Get ID
            getline(inFile, line);
            newPerson->ID = line;
            
            //Get address
            getline(inFile, line);
            newPerson->address = line;
    
            newPerson->next = NULL;
            
            if (!head)
               head = newPerson;
            else
            {
                person_ptr = head;
                
                while (person_ptr->next)
                   person_ptr = person_ptr->next;
                   
                person_ptr->next = newPerson;
            }
            
            
        }
    When I run my query and the user says 'yes' to adding a new person, the cout for entering the persons name shows up, but then skips straight to the cout for the persons ID, without any cin for the name. The cin comes for ID, then goes to cout for address, cin address, query, loop. It looks to me like they're all the same, so why is it skipping the name cin?

    Code:
        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)
             {
                node *newPerson;
                node *person_ptr;
                newPerson = new node;
            
                //Get name
                cout << "Enter the name for the person to be added formatted as last_name, first_name" << endl;
                getline(cin, line);
                newPerson->name = line;
                
                //Get ID
                cout << "Enter the ID number for " << newPerson->name << endl;
                getline(cin, line);
                newPerson->ID = line;
                
                //Get address
                cout << "Enter the entire address for the person to be added formatted as \"123 Main, Bigcity, SmallState Zipcode\"" << endl;
                getline(cin, line);
                newPerson->address = line;
        
                newPerson->next = NULL;
                
                if (!head)
                   head = newPerson;
                else
                {
                    person_ptr = head;
                    
                    while (person_ptr->next)
                       person_ptr = person_ptr->next;
                       
                    person_ptr->next = newPerson;
                }
                cout << "Would you like to add another person to this list?" << endl;
                cin >> add_resp;
                if (add_resp == "no")
                   keepgoing = 0;
             }
        }

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Do you really have to manage the list yourself or could you just use the STL list container?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    I'm not sure what you mean, nor am I familiar with the STL list container.

    Looked up in a reference, and I could use these, yes...but I'm not entirely sure how...

    Open to suggestions
    Last edited by mikeman; 01-21-2010 at 02:43 PM.

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    hk_mp5kpdw is talking about this. It already has the functions and ability to maintain a list, and would manage objects/memory better than an average user/developer defined linked list.

    EDIT: you use them exactly like you are trying to now, except all the work of creating the link list class is done for you.
    Last edited by scwizzo; 01-21-2010 at 09:49 PM.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    Yeah, I looked that up and read through it.

    What I need is to read info in from a txt file, store the info in linked list, show it to user, prompt user as to whether they want to add new nodes (persons name, id, address as members), then I have to sort it according to id (member within the nodes) and allow the user to search it according to a last name (part of the persons name, which is a member of the nodes).

    It's due tomorrow though, and I've put way more time into trying to learn this than it should take. I can't get my head around how to do sort or search, and have several other minor issues (namely centered on the one I asked about with the last line printing multiple times due to loop). Oh well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  2. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21