# Thread: Guessing Game Problems

1. ## Guessing Game Problems

I'm having problems with my guessing game code. It generates an error on line 39, the ELSE statement. I'm not sure why, the code is a type and run from my Sams Teach Yourself C book, so it should work. Can someone please explain my mistake to me? I'm using Bloodshed Dev C++ if that makes any difference.

PHP Code:
``` /* Find_nbr.c * Purpose:  This program picks a random number and then *           lets the user try to guess it * Returns:  Nothing */ #include <stdio.h> #include <stdlib.h> #include <time.h> #define NO  0 #define YES 1 void main( void ) {     int guess_value = -1;     int number;     int nbr_of_guesses;     int done = NO;          printf("\n\nGetting a Random number\n");          /*use the time to seed the random number generator */     srand( (unsigned) time( NULL ) );     number = rand();          nbr_of_guesses = 0;     while ( done == NO )     {         printf("\nPick a number between 0 and %d> ", RAND_MAX);         scanf( "%d", &guess_value ); /* Get a number */                  nbr_of_guesses++;                  if ( number == guess_value );         {                 done = YES;         }         else         if ( number < guess_value )         {                 printf("\nYou guessed too high!");         }         else         {                 printf("\nYou guessed too low!");         }     }          printf("\nCongratulations! you guessed right in %d Guesses!",         nbr_of_guesses);     printf("\n\nThe number was %d\n\n", number); }  ```

2. I am not sure if this is the reason for your problem but, using msvc++ 6, I had to chande the following code from.
Code:
```else
if ( number < guess_value )```
To the following to get your program to work.
Code:
`else if ( number < guess_value )`
I hope that this is will help you.

-jlbshecky

p.s. you might get rid of the semi-colan after the 'if' first statement to make the program properly run.

3. Originally posted by JLBSchreck
I am not sure if this is the reason for your problem but, using msvc++ 6, I had to chande the following code from.
You shouldn't have to. Whitespace is ignored. Your compiler shouldn't care if you have else and if seperated by multiple lines.

Quzah.

4. ## Re: Guessing Game Problems

Originally posted by Undeadenemy
I'm having problems with my guessing game code. It generates an error on line 39, the ELSE statement. I'm not sure why, the code is a type and run from my Sams Teach Yourself C book, so it should work. Can someone please explain my mistake to me? I'm using Bloodshed Dev C++ if that makes any difference.
use int main and return 0 or similar

if ( number == guess_value ) /* get rid of the semi here */ ;

5. It was the semicolon after the IF statement. The program runs correctly now, but while analysing the code I noticed that I never defined RAND_MAX. This leads me to believe that RAND_MAX is a keyword, but that is doubtful. Is there any way that I can lower the value of RAND_MAX, because right now it stands at 32767. I could never guess a friggin random number in that broad an area!

6. On second thought, I just ran through that and with the computer telling me "Too high" or "Too Low" it wasnt that bad. I would still like to know about that RAND_MAX thing though.

7. Originally posted by Undeadenemy
On second thought, I just ran through that and with the computer telling me "Too high" or "Too Low" it wasnt that bad. I would still like to know about that RAND_MAX thing though.
RAND_MAX is a defined value that is used to define the (obviously) maximum number ever returned by rand(). You yourself do not define it. There are a number of items defined to set values which exist in the C language itself, or the implementation of said language.

Read the FAQ if you want to know how to generate ranges of number. But in short, it's:

number = rand() % SOMETHING;

And you'll get a number from 0 to SOMETHING - 1.

Quzah.

8. >I would still like to know about that RAND_MAX thing though.

This might be of interest. Or this.

Popular pages Recent additions