-
Isdigit Problem
I want to take the string tokens and test if they are integers. If not, an exception should be thrown. According to isdigit - C++ Reference , isdigit should return 0 if it is not a digit. But it is returning 0 for anything I input. Why is this? The code snippet is below.
Code:
BinaryTree BinaryTree::setTree(char* str) {
char* pStr = strtok(str, "()");
BinaryTree result;
while(pStr != NULL) {
int token = atoi(pStr);
if(isdigit(token) == 0) {
std::cout<<pStr<<" is nTree"<<std::endl;
std::cout<<token<<" is not an int."<<std::endl;
throw InvalidInputException();
}
-
You're probably confused because isdigit()'s parameter is prototyped as int c. isdigit() returns true if c is in the range of '0' - '9', in other words, treat it like the parameter is a char.