关于while(cin>>)的问题 [英] the question on while (cin >> )

查看:131
本文介绍了关于while(cin>>)的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用cin从输入流中读取字词,例如

I used "cin" to read words from input stream, which like

int main( ){
     string word;
     while (cin >> word){
         //do sth on the input word
     }

    // perform some other operations
}

代码结构与上面的一样。它是可编译的。在执行期间,我继续输入像
aa bb cc dd

the code structure is sth like the above one. It is compilable. During the execution, I keep inputting something like aa bb cc dd

我的问题是如何结束这个输入?换句话说,假设文本文件只是aa bb cc dd。但我不知道如何让程序知道文件结束。感谢您的帮助。

My question is how to end this input? In other words, suppose the textfile is just "aa bb cc dd". But I do not know how to let the program know that the file ends. Thanks for helping.

推荐答案

您的代码是正确的。如果你是交互式输入,你需要发送一个EOF字符,如CTRL-D。

Your code is correct. If you were interactively inputting, you would need to send a EOF character, such as CTRL-D.

在您读取文件时,不需要此EOF字符。这是因为一旦你敲击输入流的结尾,没有什么剩余的cin(因为流现在关闭),因此while循环退出。

This EOF character isn't needed when you are reading in a file. This is because once you hit the end of your input stream, there is nothing left to "cin"(because the stream is now closed), thus the while loop exits.

这篇关于关于while(cin>>)的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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