We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- C++
- Classes
- Inherited Code
- Discussions
Inherited Code
Inherited Code
Sort by
recency
|
216 Discussions
|
Please Login in order to post a comment
include
include
include
include
using namespace std;
/* Define the exception here */
class BadLengthException : public exception { private: int length; // This variable stores the length of the too-short username public: // Constructor to initialize the length BadLengthException(int n) : length(n) {}
};
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; }
why do we have to inherit exception why cant we do it like as below?
class BadLengthException { int length;
};
This exercise is broken for C++20, with the editor having completely different reset contents than for C++11 or 14.
For C++20 the editor only shows an empty main() template.