cin输入错误处理

来源:互联网 发布:美国奥克兰 知乎 编辑:程序博客网 时间:2024/04/30 13:06

开始学习cin, cin.get( ), cin.getline( )觉得还好。直到昨天错误地写了一个代码的时候,发现输入不正确了。然后对这个问题进行了一些总结。


cin(输入是以回车键结束,遇到空格停止读取)

cin是从缓冲区读取数据的,那么当缓冲区有残留的数据的时候,按理来说cin也应该从缓冲区读取,并会跳过键盘输入的这个过程。
代码:

#include<iostream>using namespace std;int main(){    char ch1,ch2,ch3;    int count = 0;    string st1,st2;    char c[20],b[20];    cout << "please intput the string1:\n";    cin >> c;    cout << "please input the string2:\n";    cin >> b;    cout << "c="<< c << "\nb="<< b << endl;    return 0;}

这里写图片描述

可以见在输入第一个字符串的时候,如果有空格出现,那么输入就停止了,cin在输入字符串的时候,只读入空格前的字符串,空格之后的字符串便留在了缓冲区。第二个cin在执行的时候,就发现缓冲区有了残留的,直接从缓冲区把残留的数据取了出来存到了st2中。这个例子很好的说明了,cin是从缓冲区读取数据的。

cin.get( )(遇到回车键结束,遇到空格读取,但是不丢弃输入队列即缓冲区的换行符号)

#include<iostream>using namespace std;int main(){    char ch1,ch2,ch3;    int count = 0;    string st1,st2;    char c[20],b[20];   /* cout << "please intput the string1:\n";    cin >> ch1;    cout << "please input the string2:\n";    cin >> ch2;    cout << "ch1="<< ch1 << "\n(int)ch2="<< (int)ch2 << endl;*/    cin.get(ch1);    cin.get(ch2);    cout << "(int)ch1=" << (int)ch1;    cout << " " << "(int)ch2="<< (int)ch2 << endl;    return 0;}

遇到空格并继续读入:(a b(回车))
这里写图片描述
遇到回车换行放入缓冲区:(a(回车))
这里写图片描述

cin.getline( )(遇到换行结束,遇到空格不结束读取,回车之后,将换行符号丢弃)

#include<iostream>using namespace std;int main(){    char ch1,ch2,ch3;    int count = 0;    string st1,st2;    char c[20],b[20];   /* cout << "please intput the string1:\n";    cin >> ch1;    cout << "please input the string2:\n";    cin >> ch2;    cout << "ch1="<< ch1 << "\n(int)ch2="<< (int)ch2 << endl;*/    cin.getline(c,20);    //cin.get(ch2);   // cout << "(int)ch1=" << (int)ch1;    //cout << " " << "(int)ch2="<< (int)ch2 << endl;    cout << c << endl;    return 0;}

这里写图片描述


然后问题来了(cin发生错误的时候处理方法)

#include<iostream>using namespace std;int main(){    char ch1,ch2,ch3;    int count = 0;    string st1,st2;    char c[20],b[20];    cin.getline(c,3);    cin >> ch1;    cout << "c=" << c <<endl;    cout << "ch1=" << ch1 << endl;    cout << "输入结束"<<endl;    return 0;}

这里写图片描述
cin.getline(c,3)在执行的时候,应该只能接受2个字符,第三个字符为空字符。如果输入超过的时候,第二个之后的字符应该还在缓存区,cin是从缓存区中读取,但是cin并没有停留在读取界面,也没有从缓存区中读入(如果将ch1初始化为一个字符,那么ch1还是为当初的字符,从此可以证明第二局的cin语句并没有执行),然后直接跳到了最后一个输出语句。此时的cin函数发生了错误,并不再读取。通过cin.clear( )可以知道,它的标志位置改变了。通过cin.clear( )复位,那么cin可以从缓冲区再把剩余的字符读取出来。

#include<iostream>using namespace std;int main(){    char ch1,ch2,ch3;    int count = 0;    string st1,st2;    char c[20],b[20];    cin.getline(c,3);    cin.clear();    cin >> ch1;    cout << "c=" << c <<endl;    cout << "ch1=" << ch1 << endl;    cout << "输入结束"<<endl;    return 0;}

这里写图片描述
此时问题解决了。然后我们在通过cin.fail( )去查看是否出错,该函数是判断流输入是否失败,失败返回1,成功返回0。若失败了,那么cin函数是不会执行的。
通过以下代码,可以看出它的返回值:

#include<iostream>using namespace std;int main(){    char ch1,ch2,ch3;    int count = 0;    string st1,st2;    char c[20],b[20];    cin.getline(c,3);    cout << "flag1=" << cin.fail()<<endl;    cin.clear();    cout << "flag2=" << cin.fail()<<endl;    cin >> ch1;    cout << "c=" << c <<endl;    cout << "ch1=" << ch1 << endl;    cout << "输入结束"<<endl;    return 0;}

这里写图片描述
总结:所以当cin发生错误的时候,可以通过错误机制,对应着去找解决方法,通常在缓冲区用的函数有cin.clear(),cin.fail(), cin.bad(), cin.good(), cin.ignore()

原创粉丝点击