简体   繁体   English

在 C++ 的无限 while 循环内的 std::cin

[英]std::cin inside a infinite while loop in C++

This doesn't work as i intended it.这不像我想要的那样工作。 Please point me what i am doing wrong here.请指出我在这里做错了什么。

The program is supposed to follow the steps below.该程序应该遵循以下步骤。 But instead, it loops infinitely without waiting for inputs (except for the first time through the loop).但相反,它无限循环而不等待输入(第一次循环除外)。

  1. it prompts me and wait for inputs (integers).它提示我并等待输入(整数)。
  2. it reads 4 integer inputs to an empty vector.它读取 4 个 integer 输入到一个空向量。
  3. it prints out the elements inside the vector.它打印出向量内的元素。
  4. clear out data inside the vector.清除向量内的数据。
  5. repeat.重复。
#include<iostream>
#include<vector>

using namespace std;

int main()
{
  vector<int> list;

  while(true){
      cout << "Enter four integers:" << '\n';
      // read 4 inputs to the empty vector while making sure there's data to read
      for (int x; cin >> x && list.size() < 4;) list.push_back(x);
      // prints out elements in the vector
      for (int x : list) cout << x << '\n';
      // clear the data inside the vector
      list.clear();
    }
}

As some have suggested, i changed the code to limit readings to 4正如一些人所建议的那样,我将代码更改为将读数限制为 4

New: for (int x; cin >> x && list.size() < 4;) list.push_back (x);新: for (int x; cin >> x && list.size() < 4;) list.push_back (x);
Old: for (int x; cin >> x;) list.push_back (x);旧: for (int x; cin >> x;) list.push_back (x);

NOTE:笔记:
If i give four integers 1 2 4 6 followed by Ctrl-D, the problem persists but somehow if i give 5 or more inputs, it reads first 4 of those inputs and the program works just fine.如果我给出四个整数1 2 4 6然后按 Ctrl-D,问题仍然存在,但不知何故,如果我给出 5 个或更多输入,它会读取这些输入中的前 4 个并且程序运行良好。 Could someone plz explain why it acts this way?有人可以解释为什么它会这样吗?

What about this:那这个呢:

#include<iostream>
#include<vector>

using namespace std;

int main()
{
  vector<int> list;

  while(true){
      // infinit loop that (every time going through the loop) ask for integers 
      // and push them into a vector and display the elements of the vector.

    cout << "Enter four integers (seperated by a space):" << '\n';

    int x = 0; 
    list.clear();
    while(list.size() < 4){
        cin >> x;
        list.push_back(x);
    }

    for (int x : list) cout << x << '\n'; 

    break; // Put rest of the code here    
  }
}

So I am still not completely clear what you want.所以我仍然不完全清楚你想要什么。 You say 'the problem persists', but what problem are you talking about?您说“问题仍然存在”,但您在说什么问题? It's such basic information but you still haven't said what the problem is.这是这样的基本信息,但你还没有说问题是什么。

So I'm taking another guess, you are talking about the failure of your program to recover after Ctrl+D.所以我再猜测一下,你说的是你的程序在 Ctrl+D 后恢复失败。 You want to enter four numbers terminated by Ctrl+D and then you want the loop to restart and you to be able to enter four numbers again.您想输入四个以 Ctrl+D 终止的数字,然后您希望循环重新开始,并且您能够再次输入四个数字。 If I haven't got that right then the rest of this answer is a waste of time.如果我没有得到正确的答案,那么这个答案的 rest 就是浪费时间。

Here's some code, the new line is commented.这是一些代码,新行已注释。

#include<iostream>
#include<vector>

using namespace std;

int main()
{
  vector<int> list;

  while(true){

    cout << "Enter four integers:" << '\n';

    for (int x = 0; cin >> x;) 
      list.push_back(x);
    cin.clear(); // clear any error state on cin

    for (int x : list)
      cout << x << '\n';  

    list = {};
  }
}

The point here is that Ctrl+D is treated as the end of input and when cin encounters it goes into an error state.这里的重点是 Ctrl+D 被视为输入的结尾,当遇到cin时会进入错误 state。 In this state reads will no longer work.在此 state 读取将不再起作用。 Normally this isn't a problem because why would you want to read anything more after the end of input.通常这不是问题,因为您为什么要在输入结束后阅读更多内容。 But you apparently (possibly) do, so calling cin.clear();但是你显然(可能)这样做了,所以调用cin.clear(); removes the cin error state and allows input to happen again.删除 cin 错误 state 并允许再次发生输入。

Ok.好的。 i found a way to work around this while having a sensible and fair mind about what i've been doing wrong in my code.我找到了一种解决此问题的方法,同时对我在代码中做错的事情保持明智和公正的态度。

First of all the everything i am saying from here to below are coming from a person so new to C++ and to programming in general.首先,我从这里到下面所说的一切都来自一个对 C++ 和一般编程如此陌生的人。 And my English skill is barely sufficient to survive the day.而我的英语水平也勉强能撑过一天。

Everyone was right about limiting number of readings the code performs cause when std::cin out of data, it reads an error (i guess, i am not sure at least for now).每个人都对限制代码执行的读取次数是正确的,因为当std::cin数据不足时,它会读取错误(我想,至少现在我不确定)。 And after limiting readings the problem was still there.在限制读数后,问题仍然存在。 And i guess it's because after all in the line for (int x; cin >> x && list.size() < 4;) list.push_back(x);我想这是因为毕竟在for (int x; cin >> x && list.size() < 4;) list.push_back(x); , the first condition that is being checked is std::cin >> x and it reads an error when you reach the end of file (EOF). ,要检查的第一个条件是std::cin >> x ,当您到达文件末尾 (EOF) 时它会读取错误。 After i change the the code to check the number of reading condition first for (int x; list.size() < 4 && cin >> x;) list.push_back(x);在我更改代码以首先检查for (int x; list.size() < 4 && cin >> x;) list.push_back(x);的读取条件数之后, the list.size() < 4 condition is checked first and when it fails it doesn't go and check the cin >> x condition because i am using the && AND operator here. ,首先检查list.size() < 4条件,当它失败时,它不会 go 并检查cin >> x条件,因为我在这里使用&& AND 运算符。 That solved my problem.这解决了我的问题。 Thank you so much for everyone's time that has put in this thread.非常感谢大家花时间在这个线程中。 I apologise for not making my question clear at first.我很抱歉一开始没有把我的问题说清楚。

#include<iostream>
#include<vector>

using namespace std;

int main()
{
  vector<int> list;

  while(true){
      cout << "Enter four integers:" << '\n';
      // read 4 inputs to the empty vector while making sure there's data to read
      for (int x; list.size() < 4 && cin >> x;) list.push_back(x);
      // prints out elements in the vector
      for (int x : list) cout << x << '\n';
      // clear the data inside the vector
      list.clear();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM