isdigit issue - help

This is a discussion on isdigit issue - help within the C Programming forums, part of the General Programming Boards category; Challenge 2: Build a number guessing game that uses input validation (isdigit function) to verify that the user has entered ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    1

    isdigit issue - help

    Challenge 2: Build a number guessing game that uses input validation (isdigit function) to verify that the user has entered a digit and not a non-digit (letter).



    Unable to get this program to work. I've tried everything.

    Code:
    //Chapter 3 Challenge 2
    //by Lori Macdonald
    
    
    #include <stdio.h>
    #include <ctype.h>
    
    main()
    
    {
    
        int iRandomNum = 0;
        int iResponse = 0;
        
        srand(time());
        
        iRandomNum = (rand() % 10) + 1;
        
        printf("\nGuess a number between 1 and 10: ");
        scanf("%d", &iResponse);
        getch();
        
        switch (iResponse) {
        
        case 1:
            if (isdigit(iResponse))
                printf("\nThank you\n");
            else {
                printf("\nYou did not enter a digit\n");
            }
        
        case 2:
            if (iResponse == iRandomNum)
                printf("\nYou guessed right\n");
            else {
                printf("\nSorry, you guessed wrong\n");
                printf("The correct guess was %d\n", iRandomNum);
                    
        }}
    getch();
    
    }
    Lori

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And what, pray tell, do you think "switch" does?

    (Oh: and you'll never be able to get a letter input via scanf("&#37;d"). Since the parameters of the assignment require that you (sort-of) accept letter input, you cannot use scanf("%d").)

    And as to the question: isdigit requires a character, not an integer.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    And someone's gonna crash without "brakes"...

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And the input for isdigit is a character (although the type is technically int, that is so that you can safely feed EOF into isdigit, and it will be different from char number 255). It will NOT tell you if the user entered something that is a valid number - it will tell you if the input is between 48 (ascii code for '0') and 57 (ascii code for '9') [and possibly some other numbers in foreign, typically Asian, fonts if you use wide characters]. Of course, due to your use of switch, it will only hit the isdigit if the number is 1, which is not in the range 48..57, so it's always going to be "not a digit".

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. isdigit()
    By kermit in forum C Programming
    Replies: 3
    Last Post: 03-19-2004, 08:59 PM
  3. I read the FAQ on isdigit() but...
    By CaptainJack in forum C Programming
    Replies: 5
    Last Post: 03-16-2004, 05:03 AM
  4. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM
  5. Correct use of isdigit
    By DocDroopy in forum C Programming
    Replies: 3
    Last Post: 08-05-2002, 07:22 AM

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