Q:

Validate a string is only digits and only a certain length

So I have an issue. 
I need to take a string (I cannot have it as an int, it has to be defined as a string) and inside of the set method, I have to validate that the number is either 9 or 13 digits long and contains only digits. 
I can validate the length using str.length() but I cannot get it to validate that the input only contains digits. 
Here is my current code: 
void Book::setISBN(string ISBN) 
{

//validate the length here  ALSO NEED TO VALIDATE ITS A NUMBER

if (ISBN.length() >= 9 && ISBN.length() <= 13) {

    this->ISBN = ISBN;
}
else {
    this->ISBN = '0';
}
}

This is the code which it will pull the information:
Book b = Book();
b.setISBN("23");
b.setAuthor("R.L. Stine");
b.setTitle("Goosebumps");
b.setPrice(25.00);

Book b2 = Book("thisisshith", "Stephen King", "IT", 20.32);

The first part, b works as the setISBN("23") will return with a zero but the b2 "thisisshith" returns just like that. I need it to return as 0 as well. If it was a set of digits between 9-13 in length then it would return correctly.
Any help would be appreciated. 
I have tried isdigit() but says it cannot convert between a string and int. 

A:

In your Book constructor, make sure it is calling setISBN() and not setting this->ISBN directly:
Book::Book(string ISBN, ...) {
    //this->ISBN = ISBN;
    this->setISBN(ISBN);
    ...
}

Then, inside of setISBN(), you can do something like this:
void Book::setISBN(string ISBN) 
{
    if (((ISBN.length() == 9) || (ISBN.length() == 13)) &&
        (ISBN.find_first_not_of("0123456789") == string::npos))
    {
        this->ISBN = ISBN;
    }
    else
    {
        this->ISBN = '0';
    }
}

If you want to use isdigit() instead, you will need a loop that checks each individual char in the string.  You can either do that manually:
#include <cctype>

void Book::setISBN(string ISBN) 
{
    if ((ISBN.length() == 9) || (ISBN.length() == 13))
    {
        for (int i = 0; i < ISBN.length(); ++i)
        {
            if (!isdigit(static_cast<unsigned char>(ISBN[i])))
            {
                this->ISBN = '0';
                return;
            }
        }
        this->ISBN = ISBN;
    }
    else
    {
        this->ISBN = '0';
    }
}

Or, you can use standard search algorithms, like std::find() or std::all_of():
#include <algorithm>
#include <cctype>

void Book::setISBN(string ISBN) 
{
    if (((ISBN.length() == 9) || (ISBN.length() == 13)) &&
        (std::find(ISBN.begin(), ISBN.end(), [](char ch){ return !isdigit(static_cast<unsigned char>(ch)); }) == ISBN.end()))
    {
        this->ISBN = ISBN;
    }
    else
    {
        this->ISBN = '0';
    }
}

#include <algorithm>
#include <cctype>

void Book::setISBN(string ISBN) 
{
    if (((ISBN.length() == 9) || (ISBN.length() == 13)) &&
        std::all_of(ISBN.begin(), ISBN.end(), [](char ch)->bool { return isdigit(static_cast<unsigned char>(ch)); }))
    {
        this->ISBN = ISBN;
    }
    else
    {
        this->ISBN = '0';
    }
}

