Search code examples
c++stringiteratorgetlineistringstream

Splitting string by spaces difficulty


My code is working except for one problem, when I run it it doesn't seem to return the first string.

    string text;
    cin >> text;
    getline(cin ,text);
    istringstream  iss(text);
    copy(istream_iterator<string>(iss),
            istream_iterator<string>(),
            ostream_iterator<string>(cout, "\n"));

So if my input was, bf "ing" filename, it will only output:

"ing" 
filename

I want it so it can output the whole line like so:

bf 
"ing"
filename

Solution

  • Assuming you want to have the entire line printed from parsing the string passed to std::istringstream you should remove first reading a separate word, i.e., remove the line

    cin >> text;