簡體   English   中英

在 C++ 的無限 while 循環內的 std::cin

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

這不像我想要的那樣工作。 請指出我在這里做錯了什么。

該程序應該遵循以下步驟。 但相反,它無限循環而不等待輸入(第一次循環除外)。

  1. 它提示我並等待輸入(整數)。
  2. 它讀取 4 個 integer 輸入到一個空向量。
  3. 它打印出向量內的元素。
  4. 清除向量內的數據。
  5. 重復。
#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();
    }
}

正如一些人所建議的那樣,我將代碼更改為將讀數限制為 4

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

筆記:
如果我給出四個整數1 2 4 6然后按 Ctrl-D,問題仍然存在,但不知何故,如果我給出 5 個或更多輸入,它會讀取這些輸入中的前 4 個並且程序運行良好。 有人可以解釋為什么它會這樣嗎?

那這個呢:

#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    
  }
}

所以我仍然不完全清楚你想要什么。 您說“問題仍然存在”,但您在說什么問題? 這是這樣的基本信息,但你還沒有說問題是什么。

所以我再猜測一下,你說的是你的程序在 Ctrl+D 后恢復失敗。 您想輸入四個以 Ctrl+D 終止的數字,然后您希望循環重新開始,並且您能夠再次輸入四個數字。 如果我沒有得到正確的答案,那么這個答案的 rest 就是浪費時間。

這是一些代碼,新行已注釋。

#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 = {};
  }
}

這里的重點是 Ctrl+D 被視為輸入的結尾,當遇到cin時會進入錯誤 state。 在此 state 讀取將不再起作用。 通常這不是問題,因為您為什么要在輸入結束后閱讀更多內容。 但是你顯然(可能)這樣做了,所以調用cin.clear(); 刪除 cin 錯誤 state 並允許再次發生輸入。

好的。 我找到了一種解決此問題的方法,同時對我在代碼中做錯的事情保持明智和公正的態度。

首先,我從這里到下面所說的一切都來自一個對 C++ 和一般編程如此陌生的人。 而我的英語水平也勉強能撐過一天。

每個人都對限制代碼執行的讀取次數是正確的,因為當std::cin數據不足時,它會讀取錯誤(我想,至少現在我不確定)。 在限制讀數后,問題仍然存在。 我想這是因為畢竟在for (int x; cin >> x && list.size() < 4;) list.push_back(x); ,要檢查的第一個條件是std::cin >> x ,當您到達文件末尾 (EOF) 時它會讀取錯誤。 在我更改代碼以首先檢查for (int x; list.size() < 4 && cin >> x;) list.push_back(x);的讀取條件數之后 ,首先檢查list.size() < 4條件,當它失敗時,它不會 go 並檢查cin >> x條件,因為我在這里使用&& AND 運算符。 這解決了我的問題。 非常感謝大家花時間在這個線程中。 我很抱歉一開始沒有把我的問題說清楚。

#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