[英]How to signify no more input for string ss in the loop while (cin >> ss)
I used "cin" to read words from input stream, which like我使用“cin”从输入流中读取单词,例如
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".
换句话说,假设文本文件只是“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.
如果是交互式输入,则需要发送 EOF 字符,例如 CTRL-D。
This EOF character isn't needed when you are reading in a file.读取文件时不需要此 EOF 字符。 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”就没有任何剩余(因为流现在已关闭),因此 while 循环退出。
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 the while
loop you cannot read further input, since the EOF is already reached.由于 Windows 上的 Ctrl-Z(以及 unix 系统上的 Ctrl-D)导致到达 EOF,并且您退出
while
循环,但在while
循环之外您无法读取进一步的输入,因为已经到达 EOF。
So to enable reading using cin
again, you need to clear eof
flag, and all other failure flags, as shown below:因此,要再次使用
cin
启用读取,您需要清除eof
标志和所有其他失败标志,如下所示:
cin.clear();
After doing this, you can start reading input using cin
once again!完成此操作后,您可以再次使用
cin
开始读取输入!
int main() {
string word;
while (cin >> word) {
// do something on the input word.
if (foo)
break;
}
// perform some other operations.
}
Hit Ctrl-Z (Ctrl-D on *nix systems) and hit enter.按 Ctrl-Z(在 *nix 系统上按 Ctrl-D)并按 Enter。 That sends an EOF and invalidates the stream.
这会发送一个 EOF 并使流无效。
cin >> some_variable_or_manipulator
will always evaluate to a reference to cin
. cin >> some_variable_or_manipulator
将始终评估为对cin
的引用。 If you want to check and see if there is more input still to read, you need to do something like this:如果您想检查并查看是否还有更多输入要阅读,您需要执行以下操作:
int main( ){
string word;
while (cin.good()){
cin >> word;
//do sth on the input word
}
// perform some other operations
}
This checks the stream's goodbit, which is set to true when none of eofbit, failbit, or badbit are set.这会检查流的 goodbit,当 eofbit、failbit 或 badbit 都没有设置时,它被设置为 true。 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.
如果读取错误,或流收到 EOF 字符(来自到达文件末尾或来自键盘上的用户按 CTRL+D),cin.good() 将返回 false,并使您摆脱环形。
Take the input from a file.从文件中获取输入。 Then you will find that the while loop terminates when your program stops taking input.
然后你会发现当你的程序停止接受输入时,while 循环就终止了。 Actually
cin
stops taking input when it finds an EOF marker.实际上,
cin
在找到 EOF 标记时停止接受输入。 Each input file ends with this EOF marker.每个输入文件都以这个 EOF 标记结束。 When this EOF marker is encountered by
operator>>
it modifies the value of internal flag eofbit
into false and consequently the while loop stops.当
operator>>
遇到这个 EOF 标记时,它将内部标志eofbit
的值修改为 false,因此 while 循环停止。
It helps me to terminate loop by hitting ENTER.它可以帮助我通过按 ENTER 来终止循环。
int main() {
string word;
while(getline(cin,word) && s.compare("\0") != 0) {
//do sth on the input word
}
// perform some other operations
}
You can make a check for a special word in input.您可以检查输入中的特殊单词。 Fe "stop":
铁“停止”:
int main( ){
string word;
while (cin >> word){
if(word == "stop")
break;
//do sth on the input word
}
// perform some other operations
}
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.
您可以获取basic_ios::eof的值,它在流结束时返回 true。
you can try this你可以试试这个
string word;
vector<string> words;
while (cin >> word) {
words.push_back(word);
if (cin.get() == '\n')
break;
}
in this way, you don't have to end with CTRL+D(Z).这样,您就不必以 CTRL+D(Z) 结尾。 program will quit while sentence end
程序将在句子结束时退出
your program doesn't take in count white spaces.您的程序不考虑空格。 make difference between cin and getline...
cin 和 getline 之间的区别...
here is an example with a trick: the program get input and prints output until you hit twice Enter to quit:这是一个带有技巧的示例:程序获取输入并打印输出,直到您按两次 Enter 退出:
#include <iostream>
#include <string>
using namespace std;
int main(){
char c = '\0';
string word;
int nReturn = 0;
cout << "Hit Enter twice to quit\n\n";
while (cin.peek())
{
cin.get(c);
if(nReturn > 1)
break;
if('\n' == c)
nReturn++;
else
nReturn = 0;
word += c;
cout << word;
word = "";
}
cout << endl;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.