Input problem

Hello, i'm rather new to C++ and i was trying to take a string of input and divide the string into an array of words.
I tried using this bit of code.
getline (cin, item, ' ');
The only problem with this code is that it simply ignores the last word of the string or so i think anyway. Like I said, I am very new to this. Is there any other way of breaking a string of input into an array of words?
Last edited on
std::cin has a delimiting character of whitespace by default. Say I do this:

cin >> input1 >> input 2;

This takes two separate words, and stores them in two separate variables.

Does this push you in the write direction?
I'm not exactly understanding what that does. Do you mind explaining a bit?
Here are a couple ways to split a string:
http://www.cplusplus.com/faq/sequences/strings/split/

If I remember right, it doesn't mention the stream extraction operator or istream_iterator, both of which default to splitting by whitespace.

For example:
1
2
3
4
5
vector< string > container;
string word;
while( cin >> word ) {
    container.push_back( word );
}


Or, my personal favorite:
1
2
3
4
5
vector< string > container;

copy( istream_iterator< string >( cin ),
      istream_iterator< string >(),
      back_inserter( container ) );

cin >> input1 >> input 2;

That accepts two variables from the user, input1 and input2. cin will input data into input until a space or line return is entered. This seperate your string into two variables as long as there is only one space between the two values.
Alright, thanks for the help guys!
Topic archived. No new replies allowed.