HackerRank C++ Inherited Code

You inherited a piece of code that performs username validation for your company's website. The existing function works reasonably well, but it throws an exception when the username is too short. Upon review, you realize that nobody ever defined the exception.

The inherited code is provided for you in the locked section of your editor. Complete the code so that, when an exception is thrown, it prints Too short: n (where n is the length of the given username).

  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <exception>
  5. using namespace std;
  6. /* Define the exception here */
  7. class BadLengthException {
  8. private:
  9. int n;
  10. public:
  11. BadLengthException(int errornumber) {
  12. n = errornumber;
  13. }
  14. int what() {
  15. return n;
  16. }
  17. };

download android app