如何在保留空格作为 C++ 中的输入的同时读取带有定界字符的用户输入

How to read user input with delimiting character while retaining white spaces as input in c++

我一直在处理所需庄园中的用户输入。我需要输入例如 "Name100/Some other name"。使用下面的代码,我可以将 '/' 之前的所有内容分配给变量 input1,但是我无法将整个字符串 'Some other name' 放入变量 input2 中,只能分配第一个单词。感谢您的帮助。

string input;
string input1;
string input2;

cout << "Please enter a Name and Another Name" << endl;     
cin >> input;

stringstream ss(input);
getline(ss, input1, '/');
getline(ss, input2);

cout << input1 << endl;
cout << input2 << endl;

输出:

请输入姓名和别名

姓名100

一些

而不是

cin >> input;

使用

std::getline(std::cin, input);

第一个将在第一个白色 space 字符处停止阅读。第二个将读取整行。