거의 알고리즘 일기장

흐름상태와 입력 유효성 검사 본문

c++ 문법

흐름상태와 입력 유효성 검사

건우권 2020. 5. 2. 18:45

사용이유

파일이나 사용자의 입력을 받을때 그게 적절하게 들어왔는지 안왔는지 알기 위해 사용.


사용법 코드

#include <iostream>
#include <bitset>
using namespace std;

void printCharacterClassification(const int& i)
{
	cout << boolalpha;
	cout << "isalnum : " << bool(isalnum(i)) << endl;
	cout << "isblank : " << bool(isblank(i)) << endl;
	cout << "isdigit : " << bool(isdigit(i)) << endl;
	cout << "islower : " << bool(islower(i)) << endl;
	cout << "isupper : " << bool(isupper(i)) << endl;
}

void printStates(const std::ios& stream)
{
	cout << boolalpha;
	cout << "good : " << stream.good() << endl;
	cout << "eof : " << stream.eof() << endl;
	cout << "fail : " <<stream.fail() << endl;
	cout << "bad : " << stream.bad() << endl;
}

void classifyCharcters(const string& str)
{
	for (auto e : str)
	{
		cout << e << endl;
		cout << "isdigit" << isdigit(e) << endl;
		cout << "isblank" << isblank(e) << endl;
		cout << "isalpha" << isalpha(e) << endl;
	}
}

int main()
{
	//while (true)
	//{
	//	//char i;
	//	//cin >> i;

	//	//printStates(cin);

	//	//cout << i << endl;

	//	/*cout << boolalpha;
	//	cout << bitset<8>(cin.rdstate()) << endl;
	//	cout << bitset<8>(istream::goodbit) << endl;
	//	cout << bitset<8>(istream::failbit) << endl;
	//	cout << !bool((cin.rdstate() & istream::failbit) != 0) << endl;*/

	//	/*printCharacterClassification(i);

	//	cin.clear();
	//	cin.ignore(1024, '\n');
	//	cout << endl;*/
	//}

	cout << boolalpha;
	classifyCharcters("1234");
	classifyCharcters("a1234");

	return 0;
}

참조

 

홍정모의 따라하며 배우는 c++ 강의

반응형
Comments