0

i tried to run this code,

#include <cs50.h>
#include <stdio.h>
#include <ctype.h>

int main()
{
    int iRandom = 0;
    int iResponse = 0;


    iRandom = (rand() % 10) + 1;

    printf("\nGuess a number between 1 and 10: \n");
    scanf("%d", &iResponse);

        if isdigit(iResponse)
        printf("\nProcessing.....\n");

        else 
        printf("You did not enter a digit!\n");


    {
        if(iResponse == iRandom)
        printf("\nYou guessed right\n");

        else
        printf("You guessed wrong\n");
        printf("\nThe correct guess was %d\n", iRandom);

    }
}

but the isdigit() function keeps processing it wrong even though my input was a digit;

/home/ubuntu/workspace/fooling/ $ ./guess

Guess a number between 1 and 10: 
5
You did not enter a digit!
You guessed wrong

The correct guess was 4

can you tell me what i did wrong?

1
  1. You are missing a pair of parentheses around this if statement

    if isdigit(iResponse)
    
  2. You are using isdigit() wrong. Its definition is

    int isdigit( int ch );

    Checks if the given character is a numeric character (0123456789). The behavior is undefined if the value of ch is not representable as unsigned char and is not equal to EOF.

    Parameters
    ch - character to classify

    Return value
    Non-zero value if the character is a numeric character, zero otherwise.

So you have to pass to it a char and it will tell you if it's a digit, not an int. The int in the definition's parameters, is because all chars can be represented as integers in C.

  1. You also have some stray curly braces.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .