Title

c++语言学习-cin.fail()

c++语言学习-cin.fail()

  • cin.fail:读取失败

代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	if(cin.fail())
	  cout<<"您的输入存在问题";
	return 0; 
}

输入

qwq

输出

您的输入存在问题

应用

  • 捕获读入异常,配上break更佳
  • 可以用来对含空格的行进行字符串的提取
 string s[size],x;
 int idx=0;
 while(1){
        cin >> x;
        if(cin.fail()) break;
        s[size] = x;
        idx ++;
    }
  • xuxu
posted @ 2021-05-12 19:28  BeautifulWater  阅读(571)  评论(0编辑  收藏  举报