什么时候输入会停止? [英] When the input will stop?

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

问题描述

大家好,我有以下问题:

Hi guys i have the following question:

string s;

std::cout << "give me a string" << std::endl;  
while (std::cin >> s){
    std::cout << "give me a string" << std::endl;
}
cin.clear();  

如果 s 是整数,那么我知道如果我键入 char ,则 while 将会中断.如果它是一个整数并键入一个 double ,则会发生同样的情况.但是 strings 呢?我在Windows的某个地方读到了这句话(就像我在Windows中的Netbeans上写的那样),您必须按ctrl + z,然后它会停止,因为它被当作一个错误的字符串.但是当我这样做时,过程冻结了,什么也没发生.

if s was an integer then i know that if i typed a char then the while would break. If it was an integer and typed a double the same would happened. But what about strings ?? Somewhere I read that in windows (as I am writting at netbeans in windows) you have to press ctrl+z and then it stops as it is taken as a false string. But when i do this , the process freezes and nothing happens.

推荐答案

您需要发送EOF信号,该信号在Netbeans中为 Ctrl + D .

You need to send the EOF signal, which in Netbeans is Ctrl + D.

但是,按两次Enter键将导致循环的条件变为false,从而导致循环停止.可以预料到:"std :: cin> value"如何?在while循环中评估?

However, hitting Enter twice will result in the condition of your loop to resolve in false, causing the loop to stop. That's excpected: How is "std::cin>>value" evaluated in a while loop?

如果这不起作用,请使用 Ctrl + z 和换行符,这是Windows中的EOF信号,如

If that doesn't work, use Ctrl + z and a newline character, which is the EOF signal in Windows, as stated here. Ctrl + D is for Linux though, but it works in your case.

EOF信号关闭输入流,从而中断循环.

The EOF signal closes the input stream, thus breaking the loop.

此外,在Netbeans中(如果上述方法不起作用),您似乎需要运行您的在命令提示符下输入应用程序即可实现此目的.

Moreover, in Netbeans (if the above doesn't work) it seems that you need to run your application from the command prompt to achieve this. Another attempt would be to "press Ctrl + z after pressing Enter", as stated here.

这篇关于什么时候输入会停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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