Cin With Spaces和“,”

时间:2018-02-26 03:23:53

标签: c++ string getline

我正在尝试弄清楚如何将用户输入的string作为单个string。此外,在此之后,用户将包括以逗号分隔的其他strings

例如,foo,Hello World,foofoo其中foo是一个string,后跟Hello Worldfoofoo

我现在拥有的,它会将Hello World分成两个strings,而不是将它们合并为一个。

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;
}

如何将Hello World作为单个字符串而不是两个字符串stringTwo

0 个答案:

没有答案