Cpp-Primer icon indicating copy to clipboard operation
Cpp-Primer copied to clipboard

ex8_13, the format function may throw out of range error

Open Sefur opened this issue 6 years ago • 0 comments

In ex8_13, the function format() use substr() but not check the string length,this may throw out of range error. I think we should add string length check in substr() function or in valid() function. And in valid() function , I think just check string first char is not correct enough. below is my improve. I check string length in valid() funciton

bool valid(const string& number)
{
    if(number.size() < 7)
        return false;
    for(const auto i : number)
        if(!isdigit(i))
            return false; 
    return true;
}

Sefur avatar Nov 13 '19 05:11 Sefur