Thread: Break Up Getline

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    10

    Question Break Up Getline

    Hello All,
    Is there anyway to break up getline by 'spaces' and insert each word into a vector?
    I have the user stop at '.'. My code is as follows - it just inserts the whole string into a vector.

    How would I get something if the user types in this:
    Break Me Up.

    into a vector (separated by words) like this

    Break
    Me
    Up

    Any help greatly appreciated! - pandadc

    Code:
    int main()
    {
       vector< string > texts;
    
       string usertext;
    
       cout << "Please enter some text." << endl;
    
       getline(cin, usertext, '.');
    
    
       texts.push_back(usertext);
    
    
       for (unsigned int i = 0; i < texts.size(); i++)
          cout << texts.at(i) << endl;
    
    return 0;
    
    }

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    If you wanted to do it manualy, use the .find() function to find the first space, and .sub_str() to a temp string variable. Then .;push_back() that into the texts vector.

    There's something in boost that can do it though, I think.

    I used this recently to do something similar. I used a * instead of a ' ' though

    Code:
    void GetCatagories ( std::string List, std::vector<std::string> &Catagories )
    {
    	if ( List != "" )
    	{
    		Catagories.clear();
    
    		int 	End	= 0,
    			Start	= 0;
    
    		while ( (End = List.find( '*', Start )) != -1 ) 
    		{
    			Catagories.push_back( List.substr( Start, End - Start ) );
    			
    			Start = End+1;
    		}
    		Catagories.push_back( List.substr( Start, List.size() - Start ) );
    	}
    }
    EDIT :: change GetCatagories to ParseToVector or something. Change List to ToBeParsed. And perhaps Catagories to VecSubStr or something. The names I gave made sense to my project at the time, but probably aren't suited to yours.
    Last edited by twomers; 09-13-2006 at 08:08 AM.

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    You can use string streams, which act like input/output streams but on given strings. You construct an input string stream with the string line that you want, then like a normal (while >> cin) loop you push back the contents of the line word by word into the vector

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <vector>
    
    using std::cout;
    using std::cin;
    using std::string;
    
    using std::vector;
    
    using std::istringstream;
    
    
    int main(int argc, char *argv[])
    {
    	string line;
    	vector<string> words;
    	while(getline(cin, line))
    	{
    		istringstream in(line);
    		string word;
    		while(in >> word)
    			words.push_back(word);
    	}
    
    	return 0;
    }

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Or pass ' ' as the delimiter to getline.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> Or pass ' ' as the delimiter to getline.

    But he wants the whole sentence, for '.' being the delimiter. Not just one word. I thought the same thing initially though

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    He can break the while loop when he finds a dot as the last char in the string.
    The loop controls the validity of getline with ' ' as the delimiter. The if() inside checks for the dot being the last character of the string.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    This is what I have so far:
    Code:
    cout << "Please enter some text." << endl;
    
       getline(cin, usertext, '.');
       int end = 0;
       int start = 0;
    
    
       end = usertext.find(" ", start);
    
       texts.push_back(usertext.substr(start, end - start));
       start = end + 1;
    
    for (unsigned int j = 0; j < texts.size(); j++)
          cout << texts.at(j) << endl;
    It reads in the first word perfectly... but I can't figure out how to get my if statement to move to the next word.

    Any suggestions?
    It reads in the

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Why don't you just use my function? or any of the other suggestions posted here?

    Code:
       string usertext = "";
       vector< string > vecText;
    
       cout << "Please enter some text." << endl;
       getline(cin, usertext, '.');
    
       GetCatagories ( usertext, vecText );
    or something. Your example doesn't work cause you're not looping it.
    Last edited by twomers; 09-13-2006 at 10:18 AM.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    awesome twomers, thanks for the help! I figured it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 02:09 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM