共用方式為


cin

指定 cin 的資料流。

extern istream cin;

傳回值

istream 物件。

備註

物件控制從標準輸入的擷取為位元組資料流。 物件一旦建構,呼叫 cin.WITH TIES 傳回 &cout

範例

在此範例中,,並在遇到非數值字元時, cin 會在資料流的位元組失效。 程式清除為位元的失效並從資料流中無效的字元刪除。執行。

// iostream_cin.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

int main()
{
   int x;
   cout << "enter choice:";
   cin >> x;
   while (x < 1 || x > 4)
   {
      cout << "Invalid choice, try again:";
      cin >> x;
      // not a numeric character, probably
      // clear the failure and pull off the non-numeric character
      if (cin.fail())
      {
         cin.clear();
         char c;
         cin >> c;
      }
   }
}
  2

需求

標題: <iostream>

命名空間: std

請參閱

參考

istream

iostream 程式設計

iostreams 慣例