为什么cin需要输入换行符? [英] Why does cin need newline to be entered?

查看:149
本文介绍了为什么cin需要输入换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此代码中:

char ch;
while(cin.get(ch))
{
    std::cerr<<"got ch:"<<ch<<std::endl;
}

为什么我必须在输入got ch:在屏幕上?

Why do I have to hit enter before the "got ch:" is printed on screen ?

有什么方法可以让字符在输入后立即回显?

Is there a way to have characters echoed right after they are typed ?

推荐答案

请参阅规范与非规范终端输入很多细节。

是的,默认情况下,您必须在任何数据可用之前输入return。

Yes, by default you have to enter return before any data is made available.

是的,有一些方法可以使用非规范输入,以便字符在输入后立即可用。然而,获取该行为的机制在Windows和基于Unix的系统之间是完全不同的。

Yes, there are ways to use non-canonical input so that characters are available as soon as they are entered. However, the mechanisms for obtaining that behaviour are radically different between Windows and Unix-based systems.

这篇关于为什么cin需要输入换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆