cin

指定全局 cin 流。

extern istream cin;

返回值

istream 对象。

备注

对象控制从标准输入的抽象化为字节流。 一旦对象构造,调用 **cin.**返回 &关系cout

示例

在此示例中,它,而当方法遇到非数值字符时,cin 将在流的失败位。 程序清除 bit 的失败并从流中清除无效字符运行。

// 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 约定