在c ++中从控制台读取未知数量的行

Sac*_*hin 3 c++ string iostream input

我使用以下循环从控制台读取未知数量的行,但它不起作用.在输入输入后,我一直按下输入,但循环不会停止.

vector<string> file;    
string line;
while(getline(cin,line)
    file.push_back(line);
Run Code Online (Sandbox Code Playgroud)

Luc*_*ore 7

因为getline即使只推入也会评价为真.

您需要将读取string与空字符串进行比较,如果为真,则将其中断.

vector<string> file;    
string line;
while(getline(cin,line))
{
    if (line.empty())
       break;
    file.push_back(line);
}
Run Code Online (Sandbox Code Playgroud)