学习C++,我不知道我在这里做错了什么

use*_*328 0 c++

我正在学习C++,并且我被要求使用while函数编写代码.代码运行,但它给出不打印行Dear ....我在这做错了什么?

    cout << "Hello! Please write your recipient and the letter, then press enter:\n";
string name{ "" };
string current{ "" };
string letter{ "" };
cin >> name;
while (cin >> current){
    if (current != name){
        letter += " " + current;
    }
}
cout << "Dear " << name << "," << letter;
keep_window_open();
return 0;
Run Code Online (Sandbox Code Playgroud)

clc*_*cto 5

要输出结果,您必须使其成为cin >> current假.要执行此操作,请使用Ctrl-D发送文件结尾(EOF)cin将导致循环停止执行.

编辑:显然在Windows中,序列是Ctrl-Z.

编辑:正如@pdw所说,cout需要刷新.这通常在有换行符时完成,但由于你没有换行符,你可以使用std::flushstd::endl:

cout << "Dear " << name << "." << letter << std::flush;
Run Code Online (Sandbox Code Playgroud)