我正在尝试从命令行获取一行作为输入。我的问题是我没有得到整条线,但它被空间标记化了。
所以,如果我输入的内容如“我喜欢数学很多”而不是
"you enterend: I like Math a lot"
我得到了以下内容:
EDITING MODE: Enter a command
i like Math a lot
you entered i
EDITING MODE: Enter a command
you entered like
EDITING MODE: Enter a command
you entered Math
EDITING MODE: Enter a command
you entered a
EDITING MODE: Enter a command
you entered lot
void enterEditingMode(){
editingMode = TRUE;
static string CMD = "\nEDITING MODE: Enter a command\n";
string input;
while(editingMode == TRUE){
cout << CMD;
cin >> input;
//we assume input is always correct
// here we need to parse the instruction
cout << "you entered " << input <<endl;
答案 0 :(得分:12)
std::getline
是一次读取一行输入的标准方法。
你可以像这样使用它:
std::getline(std::cin, string);
它返回对输入流的引用,该引用隐式转换为void*
,因此您可以像这样轻松地检查成功:
if (std::getline(std::cin, string))
{
// successfully read a line...
}
答案 1 :(得分:1)
cin.getline(input);
有关详细信息,请参阅http://www.cplusplus.com/reference/iostream/istream/getline/。