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()
- cin输入错误处理
- cin标准输入流错误处理
- cin标准输入流的错误处理
- cin输入操作处理
- std::cin.getline()输入缓冲区溢出错误处理
- cin错误处理
- cin的错误处理
- 解决cin输入类型错误
- cin深入分析(上) - cin输入操作处理【转载】
- cin深入分析(上) - cin输入操作处理(转)
- cin标准输入流错误清理
- cin标准输入流错误清理
- C++中cin输入错误的解决办法
- cin.get()处理输入的问题
- [C++] cin空白换行输入处理
- cin深入分析(下) - cin的错误处理【转载】【补充】
- cin深入分析(下) - cin的错误处理(转)
- cin深入分析(下) – cin的错误处理
- 人工智能当道,你离失业还有多远
- 《设计模式之禅》读书笔记(一)——单例模式
- C++ 字符串分割的一些记录
- 终于躲不过,你还是要得是个全站工程师,论3DMax入门
- 写了 15 年代码,总结出提升 10 倍效率的三件事
- cin输入错误处理
- Android进阶之路
- 将10进制整数转为64进制形式的PHP实现
- Qt--模块划分
- java快速排序代码实现
- 每日一练21
- HashMap之HashSet和TreeSet
- 听说AI不光能做双十一海报,还有了集体智慧 | TED演讲
- RNN学习第二讲-通过Python,numpy 和 theano实现一个RNN网络