Inherited Code Discussions | C++ | HackerRank

Inherited Code

Sort by

recency

|

228 Discussions

|

  • + 0 comments
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <exception>
    using namespace std;
    
    
    class BadLengthException : public exception
    {
    private:
        string msg;
    
    public:
        BadLengthException(int length) : msg(to_string(length)) {}
    
        virtual const char *what() const noexcept override
        {
            return msg.c_str();
        }
    };
    
    
    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;
    }
    
  • + 0 comments
    class BadLengthException : public exception 
    {
    private:
        string message;
    public:
        explicit BadLengthException(int n) 
            : message(to_string(n)) {}
        
        const char* what() const noexcept override {
            return message.c_str();
        }
    };
    
  • + 1 comment

    This is my exception, it doesn't work on random test cases, anyone knows why ?

    class BadLengthException: public exception { private: int length;

    public:
        BadLengthException(int len) : length(len) {}
    
        virtual const char* what() const noexcept {
            stringstream ss;
            ss << length;
            return (ss.str().c_str());
        }
    

    };

  • + 0 comments

    Here is Inherited Code problem solution in C++ - https://programmingoneonone.com/hackerrank-inherited-code-solution-in-cpp.html

  • + 0 comments

    I'm also facing this issue on my dunkin donuts menu site.