Validate input as 0's or 1's in string array

Wanting to know how to check that 9 characters entered into a string variable are either a '0' or '1'. Want to convert string to c-string array and validate each of the nine characters. main part of code below:

//change string to c-string array to prepare for validating for 0 or 1
//int sizeArray = valBitset.size();
strcpy (bitsetArray, valBitset.c_str()); //copy valbitset as c-string to bitsetArray

//validate for 0's or 1's
for (int i=0; i<SIZEARRAY; i++)
{

if(strcmp (bitSetArray[i], '0') == 0 || strcmp (bitSetArray[i], '1') == 0)
{
cout << "Invalid characters. Must be a 0 or 1.\n";

}
No need to convert anything to a C string:
1
2
3
4
5
for (unsigned i = 0; i < valBitset.size(); ++i)
{
    if (valBitset[i] != '0' && valBitset[i] != '1')
        cout << "Invalid character found\n";
}

Alternatively, use find_first_not_of:
1
2
if (valBitset.find_first_not_of("01") != string::npos)
    cout << "Invalid character found\n";

Topic archived. No new replies allowed.