C++ c++;停止在ctrl-d上请求输入

C++ c++;停止在ctrl-d上请求输入,c++,input,getline,C++,Input,Getline,我正在尝试读取用户输入,直到按下ctrl-d。如果我是正确的,ctrl+d会发出一个EOF信号,因此我尝试检查cin.EOF()是否为真,但没有成功 这是我的密码: string input; cout << "Which word starting which year? "; while (getline(cin, input) && !cin.eof()) { cout << endl; ... cout << "

我正在尝试读取用户输入,直到按下ctrl-d。如果我是正确的,ctrl+d会发出一个EOF信号,因此我尝试检查
cin.EOF()
是否为真,但没有成功

这是我的密码:

string input;
cout << "Which word starting which year? ";
while (getline(cin, input) && !cin.eof()) {
    cout << endl;
    ...
    cout << "Which word starting which year? ";
}
字符串输入;

cout所以您想一直读到EOF,只需使用while循环和getline即可轻松实现:

std::string line; 
while (std::getline(std::cin, line))
{
    std::cout << line << std::endl;
}
std::字符串行;
while(std::getline(std::cin,line))
{

std::我是否认为
std::endl
在这里是多余的,因为不需要刷新流。可能是旧的
'\n'
?endl是性能的来源problems@Incomputable这是一个过早的优化。
std::getline()
也可能为其他类型的故障返回坏流(但如果
std::cin
连接到终端,则极有可能出现EOF)。