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'm suppose to use the isdigit to make sure it's a digit and not a letter yet when i type in the letter G for my "number" is doesn't prompt that it was not a number like it's suppose to.
Here is my code:
#include <stdio.h>
#include <ctype.h>
main()
{
int user_input_num=0;
int random_number=0;
srand( time(NULL) );//#include<ctime.h>
random_number= (rand() % 10) + 1;
printf("\n Pick a Number any number between 1 and 10: ");
scanf("%d", &user_input_num);
if (isdigit(user_input_num) ==0)
printf ("\n Processing Your Number...\n");
else
printf("You did not enter a DIGIT silly!");
if ( user_input_num == random_number)
printf("\nYou guessed the right answer!\n");
else
{
printf("\n Bzzt! Wrong answer!\n\t");
printf("The Correct answer was %d\n",random_number );
}
}
Edited by big-tony, 27 April 2009 - 05:16 PM.


Sign In
Create Account


Back to top









