Isdigit Problem

This is a discussion on Isdigit Problem within the C++ Programming forums, part of the General Programming Boards category; I want to take the string tokens and test if they are integers. If not, an exception should be thrown. ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    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();
            }

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    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.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with isdigit()
    By Mastrchief in forum C++ Programming
    Replies: 5
    Last Post: 10-14-2007, 02:19 PM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21