Thread: Guess the number game

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    2

    Guess the number game

    Hi there

    Fairly new to programming and c, I decide to make a guess number game but I'm having some trouble with it, the random number is always 0 when the user guesses the right number and then chooses to play again, if the user selects 2 to exit it won't exit the program, here's my code, any help appreciated.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <time.h>
    #include <stdbool.h>
    void headerMessage();
    void getGuess(bool,int,int);
    void outOfBounds(int);
    void tooLow();
    void tooHigh();
    void guessNumber();
    void guessesLeft();
    void checkGuessAttempts();
    void playAgain();
    void outOfBounds2();
    void goodBye();
    
    
    static int selectedNumber;
    static int userNumber;
    static bool gameOver = false;
    static bool keepGoing = true;
    static int playagain;
    static int const MAX_GUESS_ATTEMPTS = 5;
    static int nTries = 0;
    static int nTriesLeft = 5;
    
    
    int main()
    {
        srand(time(NULL));
    
    
        headerMessage();
        getGuess(gameOver,userNumber,selectedNumber);
    
    
        return 0;
    }
    
    
    void headerMessage(){
        printf("\n********************");
        printf("\n*   Guess The      *");
        printf("\n*   Number Game    *");
        printf("\n********************");
    }
    
    
    void getGuess(bool gameOver,int userNumber,int selectedNumber){
        do{
           //selectedNumber = rand()%20+1;
           printf("\n%d" , selectedNumber);
           printf("\nGuess a number between 1 and 20: ");
           scanf("%d", &userNumber);
           outOfBounds(userNumber);
    
    
    
    
        }while(gameOver == false);
    }
    
    
    void outOfBounds(int userNumber){
        if(userNumber < 0 || userNumber > 20){
            printf("NUMBER ENTERED OUT OF BOUNDS! ");
        }
        else{
            guessNumber(userNumber);
        }
    }
    
    
    void tooLow(){
        printf("TOO LOW! TRY AGAIN!");
    }
    
    
    void tooHigh(){
        printf("TOO HIGH! TRY AGAIN! ");
    }
    
    
    void guessNumber(int userNumber){
        if(userNumber < selectedNumber){
            tooLow();
            nTries++;
            nTriesLeft--;
            guessesLeft();
        }
        else if(userNumber > selectedNumber){
            tooHigh();
            nTries++;
            nTriesLeft--;
            guessesLeft();
        }
        else{
             printf("You got it! press 1 to play again or 2 to quit: ");
             printf("It took you %d ", nTries);
             scanf("%d", &playagain);
             nTriesLeft = 5;
             nTries = 0;
             playAgain();
        }
        checkGuessAttempts();
    }
    
    
    void guessesLeft(){
        printf("You have %d ", nTriesLeft );
    }
    
    
    void checkGuessAttempts(){
        if(nTries >= MAX_GUESS_ATTEMPTS){
            printf("GAME OVER! ");
            gameOver = true;
        }
    }
    
    
    void playAgain(){
        while(playagain !=1 && playagain !=2){
            outOfBounds2();
            scanf("%d", &playagain);
        }
        if(playagain == 2){
            gameOver = true;
            goodBye();
        }
        else{
            selectedNumber = rand()%20+1;
        }
    
    
    }
    
    
    void outOfBounds2(){
        if(playagain < 1 || playagain > 2){
            printf("OUT OF BOUNDS CHOICE, ONLY SELECT OPTION 1 OR 2! ");
        }
    }
    
    
    void goodBye(){
        printf("Goodbye! ");
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'd start with this suggestion: don't use any global variables (global constants are find). They make it harder to reason about your program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    2
    Any other comments or help you can offer ?

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Why do you only call rand() in play_again()?

    I think you need a "play_game()" function, that picks the target, then loops while the user enters guesses.

    So you have a upper-level function that calls play_game() then askes if the user want to play again, and the inner function that is actual game logic.

  5. #5
    Registered User
    Join Date
    Oct 2020
    Posts
    19
    A little late, but you seem to be defining quite a few unnecessary functions. The code in headerMessage() can be cut-and-pasted directly into the nearly empty main() function for sure. I really don't think you need to define functions to print "Goodbye" or "Too low" or "Too high", printf() has a purpose.

    Another suggestion is to replace "while gameOver == false" with "while !gameOver", where '!' is the logical NOT operator.

    I also second what laserlight said about not using global variables, put them in main() or wherever they're needed. I think you can replace "static int const MAX_GUESS_ATTEMPTS = 5;" with the preprocessor directive "#define MAX_GUESS_ATTEMPTS 5" as well.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Also find out the difference between using static variable vs a global one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game: Computer Guess My Number
    By Laythe in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2012, 11:25 PM
  2. Guess number game (simple)
    By Fotis in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2011, 05:00 PM
  3. Replies: 7
    Last Post: 10-19-2011, 08:45 AM
  4. Guess my Number game! check it out
    By Yosif in forum Game Programming
    Replies: 1
    Last Post: 06-09-2007, 09:43 PM
  5. Guess the number game
    By Ninestar in forum C Programming
    Replies: 12
    Last Post: 12-08-2005, 11:30 AM

Tags for this Thread