Jump to content

Generate a Random number and have user guess it

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
big-tony

big-tony

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
This is the assignment I have to do:

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.


#2
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
try using srand( time(NULL) );//#include<ctime.h>

#3
big-tony

big-tony

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

solartic said:

try using srand( time(NULL) );//#include<ctime.h>

Thanks that fixed my number generating problem. :) Now I need to see why whenever I enter a letter instead of a number, it don't trigger the (isdigit()..

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
The reason for your problem is that the random number generator uses a deterministic algorithm; if you give it the same seed two different times, it will generate the exact same numbers. That's why you seed it with the time.

#5
big-tony

big-tony

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

dargueta said:

The reason for your problem is that the random number generator uses a deterministic algorithm; if you give it the same seed two different times, it will generate the exact same numbers. That's why you seed it with the time.

I've fixed the problem with the random nymber being the same, The problem I have now is using the isdigit to connect with my error statement . Right now I could enter the letter G when it asks to Enter a NUMBER and it won't call my error statement

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
Your if statement is wrong. See my corrections below.

1) main should be declared as returning an int.
2) isdigit returns nonzero if the argument passed is a digit, zero if it's an invalid digit.
3) Your code processes the answer, even if a valid digit wasn't entered.

#include <stdio.h>
#include <ctype.h>
int main(void)
{
    int user_input_num=0;
    int random_number=0;

    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))
        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\tThe Correct answer was %d\n",random_number);

    return 0;
}


#7
big-tony

big-tony

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
Your corrected code makes the random number always stay "1" and makes ANY input whether number or letter trigger the isdigit error

#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
EDIT: I just realized why isdigit doesn't work. Get the


Sorry, forgot to include the seed...
#include <stdio.h>
#include <ctype.h>
#include <time.h>
int main(void)
{
    int user_input_num= -1;
    int random_number= 0;

    srand(time(NULL));
    random_number= (rand() % 10) + 1;

    printf("\n Pick a Number any number between 1 and 10: ");
    scanf("%d", &user_input_num);

    if(user_input_num == -1)
    {
        printf("You did not enter a DIGIT silly!\n");
        return -1;
    }

    if ( user_input_num == random_number)
        printf("\nYou guessed the right answer!\n"); 
    else
        printf("\n Bzzt! Wrong answer!\n\tThe Correct answer was %d\n",random_number);

    return 0;
}

As for the isdigit thing, look at this.

#9
big-tony

big-tony

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

dargueta said:

EDIT: I just realized why isdigit doesn't work. Get the


Sorry, forgot to include the seed...

#include <stdio.h>

#include <ctype.h>

#include <time.h>

int main(void)

{

    int user_input_num= -1;

    int random_number= 0;


    srand(time(NULL));

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


    printf("\n Pick a Number any number between 1 and 10: ");

    scanf("%d", &user_input_num);


    if(user_input_num == -1)

    {

        printf("You did not enter a DIGIT silly!\n");

        return -1;

    }


    if ( user_input_num == random_number)

        printf("\nYou guessed the right answer!\n"); 

    else

        printf("\n Bzzt! Wrong answer!\n\tThe Correct answer was %d\n",random_number);


    return 0;

}


As for the isdigit thing, look at this.
It finally works PERFECT! Thanks! :D

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
You're welcome. The reason why isdigit doesn't work is because it looks at the ASCII value of a char typecasted to an int. If it's 1-10, that doesn't correspond to a digit character, but rather some non-printing control characters, such as TAB and LF. scanf will ignore input that doesn't match the type specifiers passed to it in the format string, so really you need to set your variables to invalid values, then check to see if they were changed or not, which is what my correction does.