# Thread: Guess the number game

1. ## 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 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));

getGuess(gameOver,userNumber,selectedNumber);

return 0;
}

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. 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.

4. 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. 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. Also find out the difference between using static variable vs a global one.