求助一个关于cin流缓冲区的问题
#include<iostream>
using namespace std;
int main()
{
int elevation;
cin>>elevation;
cout<<elevation<<endl;
cout<<cin.good()<<endl;
cout<<cin.fail()<<endl;
cout<<cin.rdstate()<<endl;
cin>>elevation;
cout<<elevation<<endl;
system("pause");
return 0;
}
输入
6a7[Enter]
输出
6
1
0
0
6
为什么第二个是字符流状态还正确?
而且重新提取流内容的时候为什么还从6开始?
[解决办法]
第一个问题,字符流状态之所以正确,是因为流读到了正确的数据,如果先输入字符,后输入数字,则输入流状态就会不正确了
istream & operator >>(int &);
这个函数可以解释第二个问题,elevation被当做引用,第一次输入结束后,它指向输入缓冲区的起始处,所以第二次能继续读到6