ca73a_c++_流的条件状态
/*ca73a_c++_流的条件状态
strm::iostate
strm::badbit //流的状态
strm::failbit //输入的状态,应该输入数字,结果输入为字符,
strm::eofbit
s.eof()
s.fail()
s.bad()
s.good()
s.clear()////恢复流的状态
s.clear(flag)
cin.ignore(200,'\n');//清除前200个字符,如果遇到了‘\n’就提前结束
s.setstate(flag)
s.rdstate()
cin.setstate(ifstream::badbit);//应该写成(istream::badbit)
//error C2027: 使用了未定义类型“std::basic_ifstream<char,std::char_traits<char>>”
//强制把流的状态变成bad方法:用的二进制位来标志它的状态:如:000111000
//cin.setstate(istream::badbit);
//error C2027: 使用了未定义类型“std::basic_ifstream<char,std::char_traits<char>>”
//cin.setstate(istream::failbit);//输入的状态,应该输入数字,结果输入为字符,
//同时改变两个
//cin.setstate(istream::badbit|istream::failbit);
//清除bad
//cin.clear(istream::badbit);
//cin.clear(istream::failbit);
//保存流的状态
istream::iostate old_state = cin.rdstate();
//然后使用old_state,
//使用完成后。再恢复
//cin.clear(old_state);
*/
/*ca73a_c++_流的条件状态
strm::iostate
strm::badbit //流的状态
strm::failbit //输入的状态,应该输入数字,结果输入为字符,
strm::eofbit
s.eof()
s.fail()
s.bad()
s.good()
s.clear()////恢复流的状态
s.clear(flag)
cin.ignore(200,'\n');//清除前200个字符,如果遇到了‘\n’就提前结束
s.setstate(flag)
s.rdstate()
cin.setstate(ifstream::badbit);//应该写成(istream::badbit)
//error C2027: 使用了未定义类型“std::basic_ifstream<char,std::char_traits<char>>”
//强制把流的状态变成bad方法:用的二进制位来标志它的状态:如:000111000
//cin.setstate(istream::badbit);
//error C2027: 使用了未定义类型“std::basic_ifstream<char,std::char_traits<char>>”
//cin.setstate(istream::failbit);//输入的状态,应该输入数字,结果输入为字符,
//同时改变两个
//cin.setstate(istream::badbit|istream::failbit);
//清除bad
//cin.clear(istream::badbit);
//cin.clear(istream::failbit);
//保存流的状态
istream::iostate old_state = cin.rdstate();
//然后使用old_state,
//使用完成后。再恢复
//cin.clear(old_state);
*/
#include <iostream>
using namespace std;
void check_cin_state(istream& is)
{
if (is.bad())//检查cin是否有严重的问题,坏了。系统级错误时
{
cout << "cin bad()" << endl;
}
else
cout << "cin is not bad()" << endl;
if (is.fail())
cout << "输入数字,但输入的其它的,如果不能转成数字,就fail" << endl;
else
cout << "cin not fail()" << endl;
if (is.eof())//判断 ctrl+z,结束符
cout << "cin eof()" << endl;
else
cout << "cin not eof()" << endl;
if (is.good())
cout << "cin good()" << endl;//没出问题,就是good
else
cout << "cin not good()" << endl;
}
int main()
{
cout << "检查cin的状态: " << endl;
//强制把流的状态变成bad方法:用的二进制位来标志它的状态:如:000111000
//cin.setstate(istream::badbit);
//error C2027: 使用了未定义类型“std::basic_ifstream<char,std::char_traits<char>>”
//cin.setstate(istream::failbit);//输入的状态,应该输入数字,结果输入为字符,
//同时改变两个
//cin.setstate(istream::badbit|istream::failbit);
//清除bad
//cin.clear(istream::badbit);
//cin.clear(istream::failbit);
//保存流的状态
istream::iostate old_state = cin.rdstate();
//然后使用old_state,
//使用完成后。再恢复
//cin.clear(old_state);
check_cin_state(cin);
cout << "请输入一个整数:" << endl;
int n;
cin >> n;
cout << "再检查一次cin的状态: " << endl;
check_cin_state(cin);
//demo2
int sum = 0, value;
//while (cin >> value) //如果输入字母,条件不成立,就退出循环了。
//{
// sum += value;
// cout << "Sum is : " << sum << endl;
//}
while (cin >> value, !cin.eof())////如果输入字母,即使条件不成立,不让退出循环的方法。
{
//!cin.eof(),逗号表达式,最后一个条件决定
if (cin.bad())
throw std::runtime_error("IO stream corrupted");
if (cin.fail())
{
cerr << "bad data,try again" << endl;
//输入错误后,cin就不能用了,及时标志flag是bad,
cin.clear();//恢复流的状态
cin.ignore(200,'\n');//清除前200个字符,如果遇到了‘\n’就提前结束
continue;
}
sum += value;
cout <<"Sum is : " << sum << endl;
}
return 0;
}