-
[C++] Exception overloadingPrograming/C and CPlusPlus 2019. 2. 25. 16:34
#include
#include #include #include using namespace std; /* Define the exception here */ class BadLengthException : public exception { public: BadLengthException(int n) { stringstream ss; ss << n; _msg = ss.str(); } const char *what() const throw() { return _msg.c_str(); } protected: string _msg; }; bool checkUsername(string username) { bool isValid = true; int n = username.length(); if(n < 5) { throw BadLengthException(n); } for(int i = 0; i < n-1; i++) { if(username[i] == 'w' && username[i+1] == 'w') { isValid = false; } } return isValid; } int main() { int T; cin >> T; while(T--) { string username; cin >> username; try { bool isValid = checkUsername(username); if(isValid) { cout << "Valid" << '\n'; } else { cout << "Invalid" << '\n'; } } catch (BadLengthException e) { cout << "Too short: " << e.what() << '\n'; } } return 0; } 'Programing > C and CPlusPlus' 카테고리의 다른 글
[C] Key Press (0) 2019.03.04 [C++] 키입력을 위해 curses.h 사용하기 (0) 2019.03.04 [C++] std::bind (0) 2016.07.27 C++ Programing (0) 2014.02.23 C++ 복사생성자/대입연산자 (0) 2012.07.04