msdn上cin的解释

来源:互联网 发布:主机品牌排行榜知乎 编辑:程序博客网 时间:2024/05/18 05:09
cin

Specifies the cin global stream.

打印
extern istream cin;
Return Value

An istream object.

Remarks

The object controls extractions from the standard input as a byte stream. Once the object is constructed, the call cin.tie returns &cout.

Example

In this example, cin sets the fail bit on the stream when it encounters non-numeric characters. The program clears the fail bit and strips the invalid character from the stream to proceed.

打印
// 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;      }   }}
0 0
原创粉丝点击