Hei
Hei

Reputation: 143

stringstream doesn't accept white space?

I have the following code:

std::stringstream ss;
ss << 1 << "a b c";
std::string result;
ss >> result;
std::cout << result << std::endl;

I see "1a" instead of "1a b c".

I read somewhere that I should have ss << std::noskip. But it doesn't help.

Any idea?

Thanks in advance.

Upvotes: 12

Views: 20387

Answers (2)

Gowtham G
Gowtham G

Reputation: 11

//Try using this for getting whitespace in string
    string input;
    cout<<"\nInput : "<<input;
    getline(cin,input);
    string result,label;
    std::stringstream sstr(input);
    while(sstr>>label){
        result=result+label+" ";
    }
cout<<"\nResult : "<<result;

Upvotes: 0

ForEveR
ForEveR

Reputation: 55887

std::getline(ss, result);

or, just get string

result = ss.str();

Upvotes: 15

Related Questions