如何在读取 c++ 输入时脱离循环

How to come out of loop while reading input in c++?

本文关键字:循环 输入 c++ 读取      更新时间:2023-10-16

我有一个代码片段,如下所示:

void func() {
    string text;
    while( std::getline(cin,text)) {
            words[i++] = text;
        }
}
输入输入时,一旦

我完成输入,我就无法退出循环。打印"回车"键无济于事。如何解决这个问题?

将检查替换为:

while( std::getline(cin,text) && text.length())

现在它成功地按下了没有文本的回车键。