Thread: Strings and lists

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    72

    Strings and lists

    I have a (hopefully) easy question. Suppose I create a list and I want to be able to have the user enter entries that will be "pushed" into the list until they enter x on a line by itself (or enter on a line by itself, I'm not picky) How would one go about this? This is what I have right now and it's just not working:

    Code:
    #include <iostream>
    #include <list>
    #include <string>
    
    int main()
    {
    	string line;
        list<string> stringList;
    
    	cout << "Provide a list of characters, press x on a line by itself" << 
    		     " when done." << endl;
        
    	if (line !="x")
    	{
    		getline(cin,line);
            stringList.push_back(line);
    	}
    
    return 0;
    }
    The compiler (VC ++ 6.0) gives some very strange error messages such as:

    warning C4786: 'std::reverse_bidirectional_iterator<std::list<std ::basic_string<char,std::char_traits<char>,std::al locator<char> >,std::allocator<std::basic_string<char,std::cha
    r_traits<char>,std::allocator<char> > > >::iterator,std::basic_string<char,std::char_trait s<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,st d::allocator<char> > &,std::basic_string<char,std::char_traits

    I am assuming this means that by doing
    my if statement I've somehow put it into some sort of prepetual loop, but I
    am not sure of how to accomplish what
    I am looking to do. Ultimately, I'd like to be able to take as much user input as the user wants to enter, stick each entry in a list and display it. I then want to implement a counting function (templated function) that will take in an item as its argument and count how many times that item occurs in the list...

    I'm just working on the simplest part first before pursuing the counting part...

    Thanks for any help that can be provided..

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I don't get that error when I put that exact code into MSVC++ 6.0. Instead, I get a bunch of other errors because you are not resolving the std namespace. Add std:: in front of cout, cin, endl, getline, string, and list everywhere in your code (or an easier but less good way is to add the line "using namespace std;" below the #includes).

    Once you do this, it should compile just fine. Then, you'll probably want to make your if statement a while loop. Voila! Just like you wanted.

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    It's suppose to be like this.
    Code:
    #include <iostream>
    #include <list>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string line;
        list<string> stringList;
    
        cout << "Provide a list of characters, press x on a line by itself"
                << " when done." << endl;
        
        getline(cin, line);
        while  (line !="x")
        {
            stringList.push_back(line);
            getline(cin, line); 
        }
    
        return 0;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    Awesome! That's exactly what I needed.. thank you very very much!

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Minor nit, you should pay attention to the state of your streams, although this is not really important for cin, in particular for cin under windows (where redirection of standard io is rare).

    Code:
    #include <iostream>
    #include <list>
    #include <string>
    
    int main()
    {
        std::string line;
        std::list<string> stringList;
    
        std::cout << "Provide a list of characters, press x on a line by itself"
                << " when done." << std::endl;
        
        while(std::getline(cin,line) && line !="x")  stringList.push_back(line);
        return 0;
    }
    It's in general a good habit to get into to have no stream operations outside of the controlling condition of a loop, or at least wrap every input operation in some form of conditional while,if,for, etc... Nothing is wrong with alphaoide's solution, except that if I wanted to be a bastard, or you were simply unlucky it can fill all available memory. If your program was named lst I could take any text file that does not end in "x" and redirect it to lst by "lst < file.txt"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help With Merging Sorted Lists of Strings
    By genjiguy in forum C++ Programming
    Replies: 3
    Last Post: 04-14-2005, 03:53 PM
  2. Merging two lists as one
    By Lone in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2005, 03:59 PM
  3. strings or arrays of characters?
    By Callith in forum C++ Programming
    Replies: 13
    Last Post: 12-26-2004, 11:28 AM
  4. Radix Sort, Strings, and Linked Lists
    By dark paladin in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 03:24 PM
  5. I need help with implementing strings???
    By atif in forum C Programming
    Replies: 8
    Last Post: 04-07-2002, 10:04 AM