简体   繁体   中英

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.
  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

New: for (int x; cin >> x && list.size() < 4;) list.push_back (x);
Old: 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. 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. 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. If I haven't got that right then the rest of this answer is a waste of time.

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. In this state reads will no longer work. 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(); removes the cin error state and allows input to happen again.

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. 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). 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); , the first condition that is being checked is std::cin >> x and it reads an error when you reach the end of file (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);, 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. 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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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