call to isdigit() function

This is a discussion on call to isdigit() function within the C Programming forums, part of the General Programming Boards category; I'm trying to get a tic-tac-toe game working and I'm running into a little trouble. Here is my code: Code: ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    43

    call to isdigit() function

    I'm trying to get a tic-tac-toe game working and I'm running into a little trouble. Here is my code:

    Code:
      char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
        printf("Enter 1 for X and 2 for O: ");
        scanf("%d",&move);
        
        if(move==1 || move== 2){
            
        printf("\n\n");
        printf("%c |%c| %c \n",board[0][0],board[0][1],board[0][2]);
        printf("----------\n");
        printf("%c |%c| %c \n",board[1][0],board[1][1],board[1][2]);
        printf("----------\n");
        printf("%c |%c| %c \n\n",board[2][0],board[2][1],board[2][2]);
        
        }
        
        do{
            printf("\nEnter a valid number 1 or 2 to continue\n");
            scanf("%d",&move);
            }
            
            while(move!=1 && move !=2);// why is this not  move!=1 || move !=2?
            
            if(isdigit(move)==0){
                printf("\nInvalid input,exiting game");
                exit(404);
            }
                
                return;
    My problem is with the isdigit() function call. the variable "move" can only be an integer(1 or 2). If anything else is entered I want the error message "Invalid input,exiting game" to be printed on to the screen. This code is not working as I thought it should. Any pointers on what I'm doing wrong?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
            while(move!=1 && move !=2);// why is this not  move!=1 || move !=2?
    If move is 1, what is the result of move != 2?
    OR is a combinatorial function that is true if either side of it is true, so what happens to the loop given the answer to the above question?
    [I'm not giving the answer straight off, because learning to THINK in boolean is important, and I'm sure the questions I've just asked will lead you to the right answer].

    Code:
    if(isdigit(move)==0)
    will always be true, since move is either 1 or 2 when you get out of the loop - and isdigit is true for values 48..57, which represents the 10 values of '0'..'9' in ASCII (assuming you are indeed using the ASCII representation, rather than one of the other character value representations that are used by 0.001% of the worlds OS/Hardware combinations).

    Since scanf("%d", ...) will not accept ANY other input than digits [well, ok, spaces and newlines are accepted and immediately ignored if present in the input], calling isdigit after scanf() is pretty meaningless. What you MAY want to do instead is to check if scanf() accepted any of the data the user entered - you do that by examining the value returned from scanf() - under normal circumstances, that would correspond to the number of accepted inputs - in the case of "%d" as the format string, the expected value would be 1. If you have a format string of "%d%d%d", then you can expect to see 3 as the result. If you get anything else, something that wasn't a digit would be in the input.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Wait a second... that is a little harsh for a real game. I mean I joke around a lot in examples and pull crap like that, but you end the game if the user doesn't put in the appropriate input!? I LOVE YOU!

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    43
    Quote Originally Posted by matsp View Post
    Code:
            while(move!=1 && move !=2);// why is this not  move!=1 || move !=2?
    If move is 1, what is the result of move != 2?
    OR is a combinatorial function that is true if either side of it is true, so what happens to the loop given the answer to the above question?
    [I'm not giving the answer straight off, because learning to THINK in boolean is important, and I'm sure the questions I've just asked will lead you to the right answer].

    Code:
    if(isdigit(move)==0)
    will always be true, since move is either 1 or 2 when you get out of the loop - and isdigit is true for values 48..57, which represents the 10 values of '0'..'9' in ASCII (assuming you are indeed using the ASCII representation, rather than one of the other character value representations that are used by 0.001% of the worlds OS/Hardware combinations).

    Since scanf("%d", ...) will not accept ANY other input than digits [well, ok, spaces and newlines are accepted and immediately ignored if present in the input], calling isdigit after scanf() is pretty meaningless. What you MAY want to do instead is to check if scanf() accepted any of the data the user entered - you do that by examining the value returned from scanf() - under normal circumstances, that would correspond to the number of accepted inputs - in the case of "%d" as the format string, the expected value would be 1. If you have a format string of "%d%d%d", then you can expect to see 3 as the result. If you get anything else, something that wasn't a digit would be in the input.

    --
    Mats
    I see your point about && and || thanks. However, I'm still a little shaky on the rest of your help. scanf("%d",...) will only accept an integer with the %d conversion specifier- understood. But I don't see how to get the error message printed to the screen if the wrong input type is found. My loop goes infinite with the wrong type. I do not see what to do to stop this from happening. I guess I need a long think.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    What error code is 404, just out of curiosity.

    Code:
    do{
            printf("\nEnter a valid number 1 or 2 to continue\n");
            if(scanf("%d",&move) != 1)
            {
               fprintf(stderr, "You buffoon! That input was not 1 or 2. Can't you read?\n");
               for(;;)
                 fputs("\a", stdout);
            }
    
    #ifdef IMPLEMENT_USELESS_CODE_BLOCKS        
            while(move!=1 && move !=2)
              ;// why is this not  move!=1 || move !=2?
            
            if(isdigit(move)==0){
                printf("\nInvalid input,exiting game");
                exit(404);
            }
    #endif            
                return;

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    43
    Quote Originally Posted by master5001 View Post
    What error code is 404, just out of curiosity.

    Code:
    do{
            printf("\nEnter a valid number 1 or 2 to continue\n");
            if(scanf("%d",&move) != 1)
            {
               fprintf(stderr, "You buffoon! That input was not 1 or 2. Can't you read?\n");
               for(;;)
                 fputs("\a", stdout);
            }
    
    #ifdef IMPLEMENT_USELESS_CODE_BLOCKS        
            while(move!=1 && move !=2)
              ;// why is this not  move!=1 || move !=2?
            
            if(isdigit(move)==0){
                printf("\nInvalid input,exiting game");
                exit(404);
            }
    #endif            
                return;
    error code 404, I guess I'm showing my noobishness. I thought that the exit() function executes termination of the program with ANY integer input (i.e) I thought that could use any integer. I used (404) because that's usually the error you get when a webpage is not where it is supposed to be.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Lol... Yeah the HTTP error was the first thing that came to mind. Which is the File Not Found error. Just use zero.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Technically, you can use any integer -- the number is returned to the operating system (some IDEs will even show you, so you know when you see "Process returned -10247716" that something bad happened). Generally 0 means everything went well, and other things mean they didn't.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The only reason I say not to assign something too arbitary is because I was noticing Solaris seems a tad picky. Perhaps EXIT_FAILURE. I know gdb will spit out the exit code. So perhaps 404 could be meaningful when debugging since you would know exactly where it exited the program.

    Most importantly is the fact that you are totally abusing the end-user for making common mistakes. I love it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

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