# Thread: An isdigit Equivalent?

1. ## An isdigit Equivalent?

I'm working on a small program with an integer array, and I'm having problems with my if condition:
Code:
```if(!isdigit(numbers[x]))
{
result = 0;
}```
I'm using cctype and cstdlib libraries but is the compiler having a problem with me using this command on an integer array, and if so, what is an equivalent command?

Thanks so much.

2. The <cctype> header is for C character types, and its isdigit function tests a character to see if it represents a digit. If you've got an integer array, it's always going to be filled with integers, so what are you asking?

3. Well, to be specific, I would like to return a value if there aren't any digits in the array element, because I'm working with more than one array and I'm using the same loop to loop through both arrays at the same time, and one array may be longer than the other.

4. If the array index is higher than the array allows, you get undefined behaviour. You can't do that. You have to check the index against the array's length.

5. Here is a snippet of what I'm talking about...
Code:
```	for(x = 0; x <= maxLength+1; x++)
{
/*		if((!isdigit(num1[x])) && (!isdigit(num2[x])))
{
temp1 = 0;
temp2 = 0;
}
else if(!isdigit(num1[x]))
{
temp1 = 0;
temp2 = (num2[x] - '0');
}
else if(!isdigit(num2[x]))
{
temp1 = (num1[x] - '0');
temp2 = 0;
}
else
*/
{
temp1 = (num1[x] - '0');
temp2 = (num2[x] - '0');
}

tempResult = temp1 + temp2 + carry;

if(tempResult > 9)
{
carry = tempResult / 10;
tempResult = tempResult &#37; 10;
}
else
{
carry = 0;
}

num1[x] = '\0';
num2[x] = '\0';

}

if(carry == 0)
{
for(x = maxLength; x >= 0; x--)
{
}
cout << endl;
}
else
{
for(x = 0; x <= maxLength+1; x++)
{
}
cout << endl;
}```
Which seems to work great up until the end of the smaller array, so you can see I tried playing with the built-in isdigit() but the compiler isn't happy about it. Do you mean to say I should include the array lengths as counters as well, so that if the array length falls below 0, the the value 0 is returned?
This is possible, but I'm hoping for a simpler command line.

6. I have fixed it! It is operational! Here is what I ended up doing:
Code:
```	for(x = 0; x <= maxLength+1; x++)
{
if((x > ar1Length) && (x > ar2Length))
{
temp1 = 0;
temp2 = 0;
}
else if((x > ar1Length) && (x <= ar2Length))
{
temp1 = 0;
temp2 = (num2[x] - '0');
}
else if((x <= ar1Length) && (x > ar2Length))
{
temp1 = (num1[x] - '0');
temp2 = 0;
}
else
{
temp1 = (num1[x] - '0');
temp2 = (num2[x] - '0');
}

tempResult = temp1 + temp2 + carry;

if(tempResult > 9)
{
carry = tempResult / 10;
tempResult = tempResult % 10;
}
else
{
carry = 0;
}

num1[x] = '\0';
num2[x] = '\0';

}

if(carry == 0)
{
for(x = maxLength; x >= 0; x--)
{
}
cout << endl;
}```
I thought things would be more complicated but not too bad. Thanks so much!

7. It doesn't have to be quite so complicated. You could use something like this:
Code:
```	for(x = 0; x <= maxLength+1; x++)
{
if(x > ar1Length)
{
temp1 = 0;
}
else {
temp1 = (num1[x] - '0');
}

if(y > ar2Length)
{
temp2 = 0;
}
else {
temp2 = (num2[x] - '0');
}

tempResult = temp1 + temp2 + carry;

if(tempResult > 9)
{
carry = tempResult / 10;
tempResult = tempResult % 10;
}
else
{
carry = 0;
}

num1[x] = '\0';
num2[x] = '\0';

}

if(carry == 0)
{
for(x = maxLength; x >= 0; x--)
{
`temp1 = (x > ar1Length) ? 0 : (num1[x] - '0');`