Guessing Game Problems

This is a discussion on Guessing Game Problems within the C Programming forums, part of the General Programming Boards category; I'm having problems with my guessing game code. It generates an error on line 39, the ELSE statement. I'm not ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    15

    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 mainvoid )
    {
        
    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( (unsignedtimeNULL ) );
        
    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. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    26
    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.
    System
    OS - Microsoft Windows XP Pro
    CPU - AMD Athlon XP 2600+
    Mother Board - Abit KV7
    RAM - 512 Mb DDR (333)

    C++
    Microsoft Visual Studio Pro. 6.0
    MSDN July 2001

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    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 */ ;
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    15
    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. #6
    Registered User
    Join Date
    May 2003
    Posts
    15
    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. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >I would still like to know about that RAND_MAX thing though.

    This might be of interest. Or this.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Guessing game: how to quit the game?
    By hzr in forum C Programming
    Replies: 5
    Last Post: 12-18-2008, 10:53 AM
  2. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  3. first opengl game, problems.
    By n3v in forum Game Programming
    Replies: 1
    Last Post: 07-11-2006, 09:22 PM
  4. Game Problems
    By Spectrum48k in forum Tech Board
    Replies: 4
    Last Post: 06-02-2004, 08:08 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21