使用cin读取空白行

问题描述 投票:-2回答:1

嗨,我正在开发一个程序,该程序从用户输入的行中接收数据并打印出某些内容

这是我的伪代码,我无法发布完整的代码,所以我将尝试翻译它。

输入应该像这样一行:

close door then whatever

我的问题在我需要换行符时出现。我希望程序在有空的新行时停止。我尝试使用getline,但输入仅在它喜欢时才起作用

close
door then whatever

(假设这些是随机词)

[当我需要我的输入像第一个一样时,并在出现新行时停止。我该如何解决?

string a;
string b;
string c;
string d;

cin>>a>>b;


while(1){

if(a == "close"){//assume that a could be a bunch of words that do different things
     cin>>c>>d>>endl;
     //do whatever if string a == open
}

else if(a.empty()){ // This is where my problem is
    return 0;
}

}
c++
1个回答
0
投票

您没有在代码中引用getline,您是否尝试过:

std::getline(std::cin, a);
© www.soinside.com 2019 - 2024. All rights reserved.