stringstream help

Ok so im just playing around with stringstream so i can see how it works and all that, no im trying to take what the user inputs and save it to the stringstream and im close, it outputs what the user has but stops at the first whitespace, what am i doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

int main()
{
    stringstream strss(stringstream::out | stringstream::in);
    string ss;

    cout << "enter a string" << endl;
    getline(cin, ss);

    while(getline(cin, ss))
    {
        strss << ss;
    }

    ss = strss.str();

    strss >> ss;

    cout << ss << endl;
}
Try telling getline() to stop at a '\n' character.

getline(cin, ss, '\n');
1
2
3
4
5
ss = strss.str(); 

strss >> ss;  // remove this line and it will work

cout << ss << endl;


The first line above puts the contents of the string stream into the string. Then, the second line overwrites all of that and just reads in the contents up to the first whitespace (that's what the >> operator does).
Remove line 23.
awesome thanks.
Topic archived. No new replies allowed.