angryweasel
angryweasel

Reputation: 376

bool function returning unexpected value

isValid("23:15") returns 0 when it should return 1

bool isValid(string s){
    int pos = s.find(":");
    if(s.length() < 4 || s.length() > 5)
        return false;
    else if(s.length() == 5)
    {
        if(s[0] > 2)
            return false;
    }
    if(s[pos + 1] > 5 )
    {
        return false;
    }
    return true;
}

Actual output = 0

Upvotes: 1

Views: 88

Answers (1)

john
john

Reputation: 88017

I'm guessing you've confused digits for numbers.

if(s[0] > 2)

should be

if (s[0] > '2')

and

if(s[pos + 1] > 5 )

should be

if (s[pos + 1] > '5')

You also need to think about what happens if s does not contain a colon.

Upvotes: 3

Related Questions