jeudi 28 mai 2015

Guessing game program using isdigit

Can someone help me find the error in my code here? I'm totally new to programming and I'm trying to make a simple guessing game that also makes use of the isdigit().

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

main()
{
int iRandom = 0;
int iGuess = 0;
srand(time(NULL));

iRandom = rand()%10 + 1;

printf("\nPlease guess a number between 1 and 10: ");
scanf("%d", &iGuess);

if (isdigit(iGuess)){
    if(iGuess == iRandom){
    printf("\nYou guessed the correct number!\n");
    }   
    else{
    printf("\nThat wasn't the correct number!\n");
    printf("\nThe correct number was %d\n", iRandom);
    }
 }
else{ 
printf("\nYou did not guess a number.\n");
}
}

The problem is, regardless of whether I enter a number or not, the program returns with "You did not guess a number." Running the gcc compiler doesn't bring up any glaring errors that I can see, either. I'm assuming that I screwed up my nested-if statements, but I still don't understand why the else portion would run if isdigit(iGuess) were evaluated as true.




Aucun commentaire:

Enregistrer un commentaire