提问人:alexander sze 提问时间:3/8/2023 最后编辑:Andreas Wenzelalexander sze 更新时间:3/8/2023 访问量:46
不了解 cin.get() 和 while 循环之间的交互
Not understanding the interaction between cin.get() and while loops
问:
所以,我是C++的绝对初学者,在阅读书籍时,我遇到了这个例子:
int character;
while ((character = cin.get())!= EOF)
cout.put(character);
因此,我决定复制它并在 Visual Studio 中对其进行测试,并遇到这种情况:
Testing sentence 1
Testing sentence 1
Testing ^z sentence 2
Testing Why is the loop still going?
Why is the loop still going?
^z
所以理论上 cout.put() 函数应该在 cin.get() 到达 ctrl-z 之后停止。但是,循环仍在继续。我什至尝试在输入末尾使用 ctrl-z,它呈现了类似的结果,也许我错过了一些关于流和与 while 循环交互的关键知识?
答:
0赞
Andreas Wenzel
3/8/2023
#1
在 Microsoft Windows 上,您必须按控制台上的程序才能注册文件结束。ENTERCTRL-ZENTER
在线的中间按压是不够的。这仅适用于行的开头。CTRL-ZENTER
这不是C++语言本身的问题,而是操作系统的问题,也可能是您正在使用的C++运行时库的问题。在其他操作系统(如 Linux 或 MacOS)上,行为有所不同。
评论