일상 코딩
[C++/CPP] 18.04. stream states, input validation 본문
C++/따배C++ 18강 입력과 출력
[C++/CPP] 18.04. stream states, input validation
polarcompass 2021. 12. 19. 00:01728x90
#include<iostream>
#include<string>
#include<cctype>
#include<bitset>
using namespace std;
void printCharacterClassification(const int &i)
{
cout << boolalpha;
cout << "isalnum " << bool(std::isalnum(i)) << endl;
cout << "isblank " << bool(std::isblank(i)) << endl;
cout << "isdigit " << bool(std::isdigit(i)) << endl;
cout << "islower " << bool(std::islower(i)) << endl;
cout << "isupper " << bool(std::isupper(i)) << endl;
}
void printStates(const std::ios &stream)
{
cout << boolalpha;
cout << "good()=" << stream.good() << endl;
cout << "eof()=" << stream.eof() << endl; // file을 다 읽었는지 확인
cout << "fail()=" << stream.fail() << endl;
cout << "bad()=" << stream.bad() << endl;
}
bool isAllDigit(const string &str)
{
bool ok_flag = true;
for ( auto e : str)
if(!std::isdigit(e))
{
ok_flag = false;
break;
}
return ok_flag;
}
bool isAllDigitByLine(const string &str)
{
bool ok_flag = true;
for ( auto e : str)
{
cout << e << endl;
cout << "isdigit " << std::isdigit(e) << endl;
cout << "isblank " << std::isblank(e) << endl;
cout << "isalpha " << std::isalpha(e) << endl;
}
return ok_flag;
}
int main()
{
// while (true)
// {
// int i;
// cin >> i;
// printStates(cin);
// cout << boolalpha;
// cout << std::bitset<8>(cin.rdstate()) << endl; // read state
// cout << std::bitset<8>(std::istream::goodbit) << endl; // 마스크
// cout << std::bitset<8>(std::istream::failbit) << endl; // 마스크
// cout << !bool((cin.rdstate() & std::istream::failbit) != 0) << endl;
// printCharacterClassification(i);
// cin.clear();
// }
{
cout << boolalpha;
cout << isAllDigit("1234") << endl;
cout << isAllDigitByLine("a 1234") << endl;
}
return 0;
}
728x90
'C++ > 따배C++ 18강 입력과 출력' 카테고리의 다른 글
[C++/CPP] 18.06. basic file i/o (0) | 2021.12.21 |
---|---|
[C++/CPP] 18.05 regular expressions, 정규 표현식 (0) | 2021.12.19 |
[C++/CPP] 18.03. 문자열 stream (0) | 2021.12.18 |
[C++/CPP] 18.02. ostream output (0) | 2021.12.17 |
[C++/CPP] 18.01. istream으로 입력받기 (0) | 2021.12.17 |