Thread: random number guessing game

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    6

    Question random number guessing game

    So, yeah, I'm pretty new at C, and am in a beginner's class. Had to do this for an assignment, and was wondering if anyone could give me some pointers.

    The instructions are:

    Create a guessing game where the computer chooses a random number between 1 and 100. The user then tries to guess that number. The computer will tell the user whether the hidden number is higher or lower than the number that was guessed. When the correct number is guessed, report back how many guesses it took to find the correct number. Then offer to play the game again.

    So far I have the following programmed:


    Code:
    /*
    @author:
    @date/version:20102405.01
    @title:Guessing Game
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
        int stime;
        long ltime;
        int thisRandomNumber;
        int userinput;
    
        /* get the current time and then send the random number */
        ltime= time(NULL);
        stime= (unsigned) ltime/2;
        srand(stime);
    
        //calculate a random number between 1 and 100 */
          thisRandomNumber= rand() %100 + 1;
    
        printf("Guess the random computer generated number:\n");
        scanf("%d",&userinput);
    
        if (userinput == thisRandomNumber){
           printf("That's the number!\n");
        }
        else if (userinput > thisRandomNumber){
        printf("Your guess is higher than the number.\n");
        }
        else if(userinput < thisRandomNumber){
        printf("Your guess is lower than the number.\n");
        }
    
        return 0;
    
    }
    It will randomly compute a number, and it will tell if the number should be higher, lower, or correct, but after doing so, the program will end and will have to be executed again. How would I get it to continue letting the user guess that same number until the correct number is inputted?

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    There are a number of ways you could do this. One way would be to use a do/while loop. Have a look at these search results - you should be able to find some decent examples of how it works.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    How about using a do while loop?

    What should the test for the loop be?

    Code:
    do {
    
    
    }while(??????????);

  4. #4
    Registered User jimtuv's Avatar
    Join Date
    May 2010
    Location
    Sylvania, Ohio
    Posts
    94
    It's funny I just did this same program. The only difference is I added options for 1-100 1-50 and 1-20.

    The do while loop is the way I went.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    6
    I wasn't too clear on do while loops before doing this, but I think I have a lot more of an understanding now. However, I keep on getting an error when I try to compile it...

    guessinggame.c: In function âmainâ:
    guessinggame.c:51: error: expected âwhileâ at end of input
    guessinggame.c:51: error: expected declaration or statement at end of input

    and here's my source code again:



    Code:
      
    
    /*
    @author:
    @date/version:20102405.01
    @title:Guessing Game
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
        int stime;
        long ltime;
        int thisRandomNumber;
        int userinput;
        int userTries;
    
        /* get the current time and then send the random number */
        ltime= time(NULL);
        stime= (unsigned) ltime/2;
        srand(stime);
    
        //calculate a random number between 1 and 100 */
          thisRandomNumber= rand() %100 + 1;
    
     userTries=0;
    
        do{
        printf("Guess the random computer generated number:\n");
        scanf("%d",&userinput);
    
        if (userinput == thisRandomNumber){
           printf("That's the number!\n");
        }
        else if (userinput > thisRandomNumber){
        printf("Your guess is higher than the number.\n");
        }
        else if(userinput < thisRandomNumber){
        printf("Your guess is lower than the number.\n");
    
        userTries++;
    
        }while (thisRandomNumber!=userinput);
    
        printf("\nCongratulations! That is the number! It took you %d tries", userTries);
    
    
        return 0;
    
     }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Find your do, then count the number of { and the number of } until you run into your while.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have a problem that you can't see easily, because you didn't indent your code correctly.
    Do you have matching curly braces on the last else statement?

    That's why indenting code correctly is SO worthwhile. You can spot problems like this, quickly.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    6
    Yeah, I discovered I was missing a "{". It seems to be running smoothe and working now.

    Thanks for all the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  2. Generating a random number?
    By Konspiracy in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 12:33 AM
  3. Testing Random Number Generator
    By Roaring_Tiger in forum C Programming
    Replies: 7
    Last Post: 08-12-2005, 12:48 AM
  4. Genetic Random Number Guessing Agent (GRNGA)
    By Keybone in forum C++ Programming
    Replies: 14
    Last Post: 05-27-2004, 11:14 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM