-
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;
}
-
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.
-
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;
}
-
Or pass ' ' as the delimiter to getline.
-
>> 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
-
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.
-
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
-
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.
-
awesome twomers, thanks for the help! I figured it out.