0

I am trying to figure out how to take a string that a user enters with space as a single string. Moreover, after that, the user will include other strings separated by commas.

For example, foo,Hello World,foofoo where foo is one string followed by Hello World and foofoo.

What I have right now, it would split Hello World into two strings instead of combining them into one.

int main()
{
    string stringOne, stringTwo, stringThree;
    cout << "Enter a string with commas and a space";
    cin >> stringOne;  //User would enter in, for this example foo,Hello World,foofoo

    istringstream str(stringOne);

    getline(str, stringOne, ',');       
    getline(str, stringTwo, ',');
    getline(str, stringThree);

    cout << stringOne; //foo
    cout << endl;
    cout << stringTwo; //Hello World <---should be like this, but I am only getting Hello here
    cout << endl;
    cout << stringThree; //foofoo
    cout << endl;
}

How do I get Hello World into stringTwo as a single string instead of two.

wzz797
  • 3
  • 2
  • 2
    Possible duplicate of [How do I tokenize a string in C++?](https://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c) – Mitch Feb 26 '18 at 03:29
  • Do you have a question for us? – R Sahu Feb 26 '18 at 03:29
  • @RSahu Sorry, my question is how do I get `Hello World` into `stringTwo` as a single string instead of two. – wzz797 Feb 26 '18 at 03:34
  • Comma separated data is handled in [How can I read and parse CSV files in C++?](https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c) – Bo Persson Feb 26 '18 at 03:40
  • @Mitchel0022 I've checked that post out, and the OP was asking about how to split the `string`. I want to combine it into one `string` – wzz797 Feb 26 '18 at 03:42
  • Please take the time to convert your posed code to a [mcve] and post that. I can see where the problems are but it will make more sense to respond with an answer after I see a MCVE. – R Sahu Feb 26 '18 at 03:46
  • @RSahu I've made some edits to my original post. – wzz797 Feb 26 '18 at 03:55
  • 1
    Use `std::getline()` instead of `operator>>` to read the user's input: `getline(cin, stringOne);` – Remy Lebeau Feb 26 '18 at 04:04

1 Answers1

1

Your input is:

foo,Hello World,foofoo

Your first line that reads input from std::cin is:

cin >> stringOne;

That line reads everything until it finds the first whitespace character to stringOne. After that line, the value of strinOne will be "foo,Hello".

In the lines

getline(str, stringOne, ',');       
getline(str, stringTwo, ',');

"foo" is assigned to stringOne and "Hello" is assigned to stringTwo.

In the line

getline(str, stringThree);

nothing is assigned to stringThree since there is nothing else left in the str object.

You can fix the problem by changing the first line that reads from std::cin so that the entire line is assigned to stringOne, not the contents up to the first whitespace character.

getline(cin, stringOne);

istringstream str(stringOne);

getline(str, stringOne, ',');       
getline(str, stringTwo, ',');
getline(str, stringThree);
R Sahu
  • 204,454
  • 14
  • 159
  • 270