250x250
Notice
Recent Posts
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
관리 메뉴

일상 코딩

[C++/CPP] 18.04. stream states, input validation 본문

C++/따배C++ 18강 입력과 출력

[C++/CPP] 18.04. stream states, input validation

polarcompass 2021. 12. 19. 00:01
728x90
#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