# Thread: Having Trouble With isdigit Exercise

1. ## Having Trouble With isdigit Exercise

I have tried for sometime now with this exercise

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). Store a random number between

1 and 10 into a variable each time the program is run. Prompt

the user to guess a number between 1 and 10 and alert the user

if he was correct or not.

I wrote the following code

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

int main()

{
int  iRandomNum;
int  iResponse;

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

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

if (isdigit(iResponse))
;
else
{
printf("You didn't enter a number\n\n");
return 0;
}

if (iResponse == iRandomNum) {
printf("\nYou guessed right\n");
}
else {
printf("\nSorry, you guessed wrong\n");
printf("The correct guess was %d\n", iRandomNum);
}
}
I can't get both if statements to work after one another. I also get a warning ,

".c(10): warning #2027: Missing prototype for 'rand'."

2. You need to seed rand() with srand() before using it, rand() is declared in stdlib.h.

scanf uses a %d format, use %c and declare iResponse as a char.

You can also check directly if isdigit is NOT a digit.
Code:
if (!isdigit(iResponse))
{
printf("You didn't enter a number\n\n");
return 0;
}

3. It's still not working but I no longer receive any warnings.

Code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main()

{
int  iRandomNum;
int  iResponse;
srand(1-10);

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

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

if (isdigit(iResponse))
;
else
{
printf("You didn't enter a number\n\n");
return 0;
}

if (iResponse == iRandomNum) {
printf("\nYou guessed right\n");
}
else {
printf("\nSorry, you guessed wrong\n");
printf("The correct guess was %d\n", iRandomNum);
}
}
I will enter a number and receive the message "you didnt enter a number." I don't see how this can be since isdigit is supposed to verify that iResponse is a digit even though its an integer to begin with.

If I turn iResponse to a char then the 2nd if will execute but fail to produce desired results since iResponse would now be a char and iRandomNum an int.

4. Originally Posted by BrandNew
If I turn iResponse to a char then the 2nd if will execute but fail to produce desired results since iResponse would now be a char and iRandomNum an int.
Sure, but if iResponse is an int how can it be anything but a digit?

What you encounter is an ascii representation of 0-9. You can do the comparison by subtracting '0' from iResponse, like this. if( (iResponse - '0') == iRandom )

Look up a ascii table, to see the reason behind this.

5. Code:
if (isdigit(iResponse))
;  <---- error
else
{
printf("You didn't enter a number\n\n");
return 0;
See the semicolon I marked as an error? What you have there is the functional equivalent of:
Code:
if (isdigit(iResponse));
which does nothing.

If you want to check if something is not a digit use the logical NOT operator...
Code:
if ( ! isdigit(iResponse))
{ printf("You didn't enter a number\n");
return 0; }
Then it will work.

6. It's not an actual error, it's functionally equivalent, but a some what unusual way of doing it.

7. Thanks for the help everyone it finally worked. The final version is :
Code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main()

{
int  iRandomNum;
char  iResponse;
srand(1-10);

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

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

if (!isdigit(iResponse)) {
printf("You didn't enter a number\n\n");
return 0;
}

if ( (iResponse - '0')== iRandomNum) {
printf("\nYou guessed right\n");
}
else {
printf("\nSorry, you guessed wrong\n");
printf("The correct guess was %d\n", iRandomNum);
}
}
For some reason
Code:
if (!isdigit(iResponse))
would always be deemed true if iResponse was declared as an int which leads me to believe isdigit shouldn't be used with int variable type

8. Ok, for a single character of input your range is 0 to 9 ... not 1 to 10. Entering 10 is two digits and overflows a single character.

This means several changes... you need to lose the +1 on your random number generator so that it can produce 0 to 9, you need to change your entry prompt...

Also as a matter of performance you should seed the random number generator before using it, otherwise it will always generate the same sequence... srand(1-10) being a fixed value will also generate the same sequence every time.
srand(time(0)); is usually adequate as the value of time() changes every second.