How to signify no more input for string ss in the loop while (cin gt;gt; ss)(如何表示循环中不再有字符串 ss 的输入 while (cin gt;gt; ss))
问题描述
我使用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
}
代码结构与上面类似.它是可编译的.在执行过程中,我不断输入类似
The code structure is something 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.
推荐答案
您的代码是正确的.如果是交互式输入,则需要发送 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.
这篇关于如何表示循环中不再有字符串 ss 的输入 while (cin >> ss)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何表示循环中不再有字符串 ss 的输入 while (cin >> ss)


基础教程推荐
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05