Thread: Entering strings into a list?

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    131

    Entering strings into a list?

    I am new to C++ and programming for that matter, but I am realizing that I love it more and more everyday. At this point I am learning about <list> and <string>. I am trying to get a program up and running that simply takes 20 string in from the user, stores them in a list, & then outputs them back to the user. Here is what I have come up with so far. From what I can tell it is takeing the strings in, but I am not sure if it is storing them in my list. Also I thought my second loop would output the strings back but I was wrong. Everything is compiling without errors, but it is not doing what I want it to do.

    Code:
    #include <iostream>
    
    #include <string>
    
    #include <list>
    
    using namespace std;
    
    int main(int argc, char **pArgv)
    
    {
    
         string useInput; 
    
         list<string> sampleList(20); 
    
         cout << "Enter twenty strings" << endl; 
    
         list<string>::iterator iter; 
    
         for(iter = sampleList.begin(); iter != sampleList.end(); iter++) {
    
              cin >> useInput; 
    
              sampleList.push_back(useInput); 
    
              }
    
         for(iter = sampleList.begin(); iter != sampleList.end(); iter++) {
    
              cout << (*iter) << endl; 
    
              }
    
    }
    Am I correct in assuming that my first for loop is taking a string in from the user and putting it in the first memory location of my list?

    What is weird and I can't figure out is when I do compile and run my program it prompts me to enter my strings, but does not stop me after I enter 20.
    Last edited by chadsxe; 06-02-2005 at 10:00 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    4
    Why dont you just use a multi dimensional array ?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    The excersie I am doing write now is geared towards this. This is not for anything specific. I am trying to learn this stuff on my own through tutorials and a few books.
    Last edited by chadsxe; 06-02-2005 at 10:01 AM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    I just had an idea and changed part of my first for loop.

    Code:
    #include <iostream>
    
    #include <string>
    
    #include <list>
    
    using namespace std;
    
    int main(int argc, char **pArgv)
    
    {
    
         string useInput; 
    
         list<string> sampleList(20); 
    
         cout << "Enter twenty strings" << endl; 
    
         list<string>::iterator iter; 
    
         for(iter = sampleList.begin(); iter != sampleList.end(); iter++) {
    
              cin >> useInput; 
    
              *iter = useInput;  // New idea
    
              }
    
         for(iter = sampleList.begin(); iter != sampleList.end(); iter++) {
    
              cout << (*iter) << endl; 
    
              }
    
    }
    Now the program terminates after I entered my 20th string. I still have not figured out why my second for loop is not outputting my string to the user.

    Is my second idea better then my first?

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    you have misused the iterator of list,

    Code:
    for(iter = sampleList.begin(); iter != sampleList.end(); iter++)
    very repeat, iter and end iterator of sampleList will be checked.

    but
    Code:
    sampleList.push_back(useInput);
    every repeat, the end iterator of sampleList will be changed, so iter will never get END, it make repeating for ever

    try it like this
    Code:
         for(int i=0; i<20; i++){
              cin >> useInput; 
              sampleList.push_back(useInput); 
              }
    at last
    Code:
    list<string> sampleList(20);
    it means creating 20 empty strings at initialization time, not allocating 20 strings emputy space.

    list<string> sampleList; is ok
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Usually you would do something like this:
    Code:
    #include <iostream>
    #include <string>
    #include <list>
    
    using namespace std;
    
    int main(int argc, char **pArgv)
    {
        string useInput; 
        list<string> sampleList; 
    
        cout << "Enter twenty strings" << endl; 
    
        while( sampleList.size() < 20 )
        {
            cin >> useInput; 
            sampleList.push_back(useInput); 
        }
    
        list<string>::const_iterator iter;
    
        for(iter = sampleList.begin(); iter != sampleList.end(); iter++)
        {
              cout << (*iter) << endl; 
        }
    
    }
    "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
    Jun 2005
    Posts
    131
    Quote Originally Posted by hk_mp5kpdw
    Usually you would do something like this:
    Code:
    #include <iostream>
    #include <string>
    #include <list>
    
    using namespace std;
    
    int main(int argc, char **pArgv)
    {
        string useInput; 
        list<string> sampleList; 
    
        cout << "Enter twenty strings" << endl; 
    
        while( sampleList.size() < 20 )
        {
            cin >> useInput; 
            sampleList.push_back(useInput); 
        }
    
        list<string>::const_iterator iter;
    
        for(iter = sampleList.begin(); iter != sampleList.end(); iter++)
        {
              cout << (*iter) << endl; 
        }
    
    }
    You said usually....

    Why usually?

    What is wrong with my method.

    Thanks

  8. #8
    Registered User
    Join Date
    May 2005
    Location
    Sweden
    Posts
    16
    Quote Originally Posted by chadsxe
    What is wrong with my method.
    The problem, as previously stated, is that your loop will continue forever.

    The end of list is not determined by how many items you initially allocated space for, but when the actual last element is reached. A list is dynamic in size, so it will just keep growing when you push new objects.

    The way you do it, every time you increase the iterator you also increase the size, preventing it from ever reaching the end.

    Also, you initially create a list which allocates 20 new std::strings using their default constructor. This means that there will be 20 empty strings in your list before anything you actually push_back.

    Try it yourself:
    Code:
    #include<iostream>
    #include<list>
    #include<string>
    
    int main()
    {
        std::list<std::string> testList(20);
        
        std::cout << testList.size() << std::endl;    // Will output 20
        testList.push_back(std::string("text"));
        std::cout << testList.size() << std::endl;    // Will output 21
    }
    and then try it with std::list's default (empty) constructor, like so:
    Code:
    std::list<std::string> anotherList;
    You'll see that in the second case, the size of the list is as expected.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Ahhhahah......

    I forgot about a list being dynamic, and that it is the programmers responsibility to control it. When pushing outside the bounds of the list were does the variable go. I am guessing it is stored in the next avaiable memory slot. Which I am also guessing would be very bad for a program.

    Thanks

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Changing values in the nodes pointed to by the iterators in your second example doesn't increase the size of the list like the calls to push_back() in your first example.

    Why the output protocol in the second example doesn't work is unclear. I'd suggest checking the first and last nodes of the list and the size of the list by using

    cout << sampleList.front() << ' ' << sampleList.back() << ' ' << sampleList.size() << endl;

    to be sure that the list is populated as you expect.
    You're only born perfect.

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    When pushing outside the bounds of the list were does the variable go?

    The good news is that if you put more entries into an STL container than were initially allocated for the STL container will automatically enlarge itself, which is often what you want. The bad news is same property of STL containers---that is sometimes you don't want them to enlarge when they reach a certain size, but they will anyway, unless you use proper syntax.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. urgent please please..
    By peter_hii in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2006, 06:35 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM