(cin>> ss)在循环中如何表示字符串ss没有更多的输入。 [英] How to signify no more input for string ss in the loop while (cin >> ss)
问题描述
我用cin从输入流中读取单词,就像
int main(){
字串;
while(cin>> word){
//做输入字
}
//执行一些其他操作
}
代码结构与上面类似。它是可编译的。在执行过程中,我一直在输入类似于
aa bb cc dd
pre>
我的问题是如何结束这个输入?换句话说,假设文本文件只是aa bb cc dd。但是我不知道如何让程序知道这个文件结束了。
解决方案你的代码是正确的。如果您是交互式输入,则需要发送EOF字符,例如CTRL-D。
当您在文件中读取时,不需要此EOF字符。这是因为一旦你打到输入流的末尾,没有什么可以cin(因为流已经关闭了),所以while循环就退出了。
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 }
The code structure is something like the above one. It is compilable. During the execution, I keep inputting something like
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.
解决方案Your code is correct. If you were interactively inputting, you would need to send a EOF character, such as CTRL-D.
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.
这篇关于(cin>> ss)在循环中如何表示字符串ss没有更多的输入。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!