如何表示循环中不再输入字符串 ss while (cin >> ss)
我使用“cin”从输入流中读取单词,
int main( ){
string word;
while (cin >> word){
//do sth on the input word
}
// perform some other operations
}
代码结构类似于上面的代码结构。它是可以编译的。在执行过程中,我不断输入类似的内容
aa bb cc dd
我的问题是如何结束这个输入?换句话说,假设文本文件只是“aa bb cc dd”。但我不知道如何让程序知道文件结束。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
由于其他人已经回答了这个问题,我想添加这一点:
由于 Windows 上的 Ctrl-Z(以及 unix 系统上的 Ctrl-D)会导致 EOF 到达,并且您会退出 while 循环,但在
while
循环之外,您无法读取进一步的输入,因为已经到达 EOF。因此,要再次使用
cin
进行读取,您需要清除eof
标志以及所有其他失败标志,如下所示:完成此操作后,您可以开始使用
eof
读取输入再次code>cin!As others already answer this question, I would like add this important point:
Since Ctrl-Z on Windows (and Ctrl-D on unix systems) causes EOF to reach, and you exit from the
while
loop, but outside thewhile
loop you cannot read further input, since the EOF is already reached.So to enable reading using
cin
again, you need to cleareof
flag, and all other failure flags, as shown below:After doing this, you can start reading input using
cin
once again!按 Ctrl-Z(在 *nix 系统上按 Ctrl-D)并按 Enter。这会发送 EOF 并使流无效。
Hit Ctrl-Z (Ctrl-D on *nix systems) and hit enter. That sends an EOF and invalidates the stream.
cin>> some_variable_or_manipulator
将始终评估为对cin
的引用。如果您想检查是否还有更多输入需要读取,您需要执行以下操作:这会检查流的 goodbit,当未设置 eofbit、failbit 或 badbit 时,该 Goodbit 将设置为 true。如果读取错误,或者流接收到 EOF 字符(到达文件末尾或用户在键盘上按 CTRL+D),cin.good() 将返回 false,并让您退出环形。
cin >> some_variable_or_manipulator
will always evaluate to a reference tocin
. If you want to check and see if there is more input still to read, you need to do something like this:This checks the stream's goodbit, which is set to true when none of eofbit, failbit, or badbit are set. If there is an error reading, or the stream received an EOF character (from reaching the end of a file or from the user at the keyboard pressing CTRL+D), cin.good() will return false, and break you out of the loop.
我猜你想在文件末尾跳出。
可以获取 basic_ios::eof 的值,最后返回 true的流。
I guess you want to jump out at the end of file.
You can get the value of basic_ios::eof , it returns true at the end of stream.
从文件中获取输入。然后你会发现当你的程序停止接受输入时,while 循环终止。
实际上,
cin
在找到 EOF 标记时停止接受输入。每个输入文件都以此 EOF 标记结尾。当operator>>遇到这个EOF标记时,它会将内部标志
eofbit
的值修改为false,从而使while循环停止。Take the input from a file. Then you will find that the while loop terminates when your program stops taking input.
Actually
cin
stops taking input when it finds an EOF marker. Each input file ends with this EOF marker. When this EOF marker is encountered byoperator>>
it modifies the value of internal flageofbit
into false and consequently the while loop stops.它可以帮助我通过按 ENTER 来终止循环。
It helps me to terminate loop by hitting ENTER.
您可以检查输入中是否有特殊单词。
Fe“停止”:
You can make a check for a special word in input.
F.e. "stop":
你可以
这样尝试,不必以 CTRL+D(Z) 结束。程序将在句子结束时退出
you can try this
in this way, you don't have to end with CTRL+D(Z). program will quit while sentence end
你的程序不接受 count 个空格。区分 cin 和 getline...
这是一个带有技巧的示例:程序获取输入并打印输出,直到您按两次 Enter 退出:
your program doesn't take in count white spaces. make difference between cin and getline...
here is an example with a trick: the program get input and prints output until you hit twice Enter to quit:
你的代码是正确的。如果您进行交互输入,则需要发送 EOF 字符,例如 CTRL-D。
当您读取文件时,不需要此 EOF 字符。这是因为一旦到达输入流的末尾,“cin”就没有什么了(因为流现在已关闭),因此 while 循环退出。
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.