Thread: char x isdigit ?

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    36

    char x isdigit ?

    Hi guys,

    I'm having trouble with the following function, can anyone spot whats wrong, I can't get atoi to work, the only work around thus far is doing this which I don't particularly like

    Code:
        if ( isdigit( x ) )
            return (x - 48);
    I want to be able to use atoi

    Code:
    int get_coord(char x)
    {
    
        const char cols[10] = "ABCDEFGHIJ";
        int i;
    
        if ( isdigit( atoi(&x) ) )
            return x;
    
        for ( i = 0; i < 10; i++ )
        {
            if ( x == cols[i] )
                return i;
        }
    
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    atoi() works on strings, not single characters.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    compiles ok but errors out.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    Quote Originally Posted by Todd Burch View Post
    atoi() works on strings, not single characters.
    Can you recommend any alternative..., would you say

    Code:
            return (x - 48);
    is bad programming practice?

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    That isn't how one goes about using atoi, first of all.

    Example
    Code:
    const char *numstr = "145";
    
    int x = atoi(numstr);
    ;

    And secondly, there is nothing wrong with isdigit() in this case. You just need to use a string, not a pointer to a char. Unless, that char contains 0, the program will likely do things you do not like... And if the char does contain 0, then nothing is done, which you still won't like.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by Cero.Uno View Post
    Can you recommend any alternative..., would you say

    Code:
            return (x - 48);
    is bad programming practice?

    Code:
    // how about..
    
    int get_coord(char *x)
    {
       if(x && isdigit(*x))
         return atoi(x);
    }

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    What are you trying to do with this function? Are you wanting to verify a value is a proper number? If so, are you really passing a single character to this routine?
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Cero.Uno View Post
    Can you recommend any alternative..., would you say

    Code:
            return (x - 48);
    is bad programming practice?
    Yes, very bad. Magic numbers make for write-only code.
    Not to mention it isn't portable since it assumes everything uses ASCII.
    This is a little better, and should be portable:
    Code:
    return (x - '0');

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    Quote Originally Posted by Todd Burch View Post
    What are you trying to do with this function? Are you wanting to verify a value is a proper number? If so, are you really passing a single character to this routine?
    returning coordinates, I'm using a matrix for a gridding system for my battleships game. I'm attempting to make the function generic, should the value be B on the display from the users input then I want 1 to be returned from the loop, however should the numbered row ( >> x << ) be a number by the user, I just want the function to return.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    Code:
     return (x - '0');
    works good.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Shouldn't a coordinate be x and y? Perhaps numbers indicating x and letters indicating y.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    this function is invoked for both inputs OF x and y.

    Code:
    x = get_coord(input[1]);
    y = get_coord(input[0]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM