错误处理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误处理相关的知识,希望对你有一定的参考价值。

#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    while(cin.fail())
    {
        cin.clear();
        cin.ignore(100,‘\n‘);
            cout<<"只允许输入数字";
        cout<<"Enter a positive integer"<<endl;
        cin>>n;
    }
    cout<<n<<endl;
    return 0;
}

 可以防止输入非数字导致的死循环。

以上是关于错误处理的主要内容,如果未能解决你的问题,请参考以下文章