In my function isValid I need to check the string phoneNumber and make sure it only contains the digits 0-9. If it does contain a letter I must set status=3. With my current code phoneNumber.find_first_not_of("02345789",0) the status is ALWAYS set to 3 even if I enter the correct values. Any guidance would be great, thanks.
int isValid ( string phoneNumber)
{
int status;
if (phoneNumber.length() != 8 && phoneNumber.length() != 11 )
{
status= 1;
}
else if ( phoneNumber.at(0) != '1')
{
status = 2;
}
else if ( phoneNumber.find_first_not_of("02345789",0))
{
status = 3;
}
else
{
status = 0;
}
return status;
}
Perfect!!! thank you so much! It worked perfectly just by adding
else if ( phoneNumber.find_first_not_of("0123456789") != std::string::npos )
{
status=3;
}
looks like i was missing the != std::string::npos
Any idea what that does? I know that the phoneNumber.find_first_not_of("0123456789")
searches the phoneNumber string for any occurrence that is not one of those digits so logically I thought it would work.
> I know that the phoneNumber.find_first_not_of("0123456789")
> searches the phoneNumber string for any occurrence that is not one of those digits
If an occurrence is found, it returns the position at which it was found: a value in [ 0, size()-1 ]
If not, it returns std::string::npos.
If phone_number.find_first_not_of("0123456789") returns a value other than std::string::npos,
it means that a character that is not one of the decimal digits was found.