使用:while(cin> x)和eof [英] use of: while (cin >> x) and eof

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

问题描述

我不太确定如何作品我想是我的根本问题在这里。我已经阅读了几个以前的帖子on(cin >> x),但没有什么似乎回答我的问题真的。
我使用这个循环来读取一些文本数据:

I am not so sure how the works I suppose is my root problem here. I've read a few previous posts on while(cin>>x) but nothing seems to answer my question really. I am using this loop to read in some text data:

while (cin >> x){
    searchText.push_back(x);
}

但是后来在代码中我想用一个单词读:

but then later in the code I am trying to read in a single word using:

cout << "Please enter your target word: ";
string targetWord;
cin >> targetWord;

但是上面的while循环/ eof似乎破坏了第二个代码片段上面的片段一切正常,但显然这不是我想做的)

but the above while loop/ eof seems to scupper the 2nd code snippet (if I move the 2nd code snippet up above it all works fine, but obviously that is not what im trying to do)

编辑
这里是完整的代码清晰度:

EDIT Here is the full code for clarity:

int main()
{
// ask for the target word
//   cout << "Please enter your target word: ";
//   string targetWord;
//   cin >> targetWord;

   // ask for and read the search text
   cout <<  "Enter the complete search text, "
            "followed by end-of-file: ";
   vector<string> searchText;
   string x;
   while (cin >> x){
      searchText.push_back(x);
   }

   // check that some text was entered
   typedef vector<string>::size_type vec_sz;
   vec_sz size = searchText.size();
   if (size == 0){
      cout << endl <<   "You must enter some text. "
                        "Please try again." << endl;
      return 1;
   }

// ask for the target word
   cin.clear();
   cout << "";
   cout << "Please enter your target word: ";
   string targetWord;
   cin >> targetWord;


   int count = 0;
   for (int i=0; i<size; ++i){
      if (targetWord == searchText[i]){
      count++;
      }
   }

   cout  << "The target word [" << targetWord << "] was "
            "in the search text " << count << " times." << endl;

   return 0;
}

我只是试图从用户那里接收一些文本...一个搜索词,并看到该词出现在输入文本中的次数(很简单!)

I am just trying to take in some text from the user... then a search word and see how many times the word appears in the entered text (pretty simple!)

我知道我可以做不同,但这里的问题更多地是关于如何在之前有EOF的情况下,我可以再次使用cout / cin流

I know I could do it differently but the question here is more about how can I use the cout/ cin stream again after it has had an EOF in it previously

推荐答案

cin (或任何其他 std :: stream )命中文件结尾,它设置一个状态以指示这已发生。

When cin (or any other std::stream) hits an end of file, it sets a status to indicate that this has happened.

要重置此状态,您需要调用 cin.clear(); 状态,并确保流是确定再次使用。这也适用于您从文件读取,并希望从头开始重新启动。

To reset this status, you need to call cin.clear();, which will clear any "bad" state, and make sure the stream is "ok" to use again. This also applies if you are reading from a file, and want to restart from the beginning.

编辑:我只是把上面的代码,并运行它在我的机器,在顶部添加

I just took the code posted above, and ran it on my machine, adding at the top

#include <iostream>
#include <vector>

using namespace std;

以下是编译和运行:

$ g++ -Wall words.cpp 
words.cpp: In function ‘int main()’:
words.cpp:40:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
$ ./a.out
Enter the complete search text, followed by end-of-file: aa bb cc [CTRL-D]
Please enter your target word: aa
The target word [aa] was in the search text 1 times.

这是我期望看到的...

which is what I expected to see...

Edit2:为了完整性:使用 cin.clear()的成功率将取决于实现。更可靠的解决方案是在程序的第一阶段使用不同的方式来标记词流的结束。可以使用单个或!或者其他不应该是字的东西 - 或者更长的东西,比如&& @&&使得很难输入和记住什么时候有15页的输入。

For completeness: The "success rate" of using cin.clear() will depend on the implementation. A more reliable solution is to use a different way to mark the end of the stream of words in the first phase of the program. One could use a single "." or "!" or some other thing that isn't supposed to be in a "word" - or something longer, such as "&&@&&", but that makes it hard to type and remember when one is 15 pages into the input.

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

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