# Thread: Guess number game (simple)

1. ## Guess number game (simple)

Hi programmers!

I'm new at c++ and i want help about a simple guessing number game.

I want:
• the player has 3 tries (the number is between 0 and 20)
• if player doesnt guess the right number, then the programm will close
• if the player guess the right number then print "congrats" and ask the player if he want a new game.
I post the code i have already write. Because i am new, any help are welcome!

Code:
```#include <stdio.h>#include <stdlib.h>

main()

{

int x;
int guess;
int tries;

do
x = rand();
while ( x < 0 || x > 20 );

tries=0;

do
do

do
scanf("%d",&guess);
while ( guess < 0 || guess > 20 );

tries=tries+1

if (guess==x)
printf ("Congratulations, you guess the right number. Do you want play again?");
else
printf ("wrong number!");

while tries=3

}```

Thank you very much.

2. You should start by adding ALL the braces you seem to have deleted from the source code.

python works off the indentation of blocks - C does not.

3. Originally Posted by Salem
You should start by adding ALL the braces you seem to have deleted from the source code.

python works off the indentation of blocks - C does not.
I'm sorry. You have right.

I wrote new code.

Code:
```#include<stdio.h>
#include<stdlib.h>

int main()
{
int random;
int usersGuess;

printf ("Welcome to the game\n\n"
"I will generate a number between 1 and 20\n\n"
"and you have 3 chances to guess it\n\n");

do
random = rand();
while ( random < 0 || random > 20 );

printf ("Guess a number between 0 and 20");
printf ("Input guess : ");
scanf("%d",&usersGuess);
int attempts = 1;
while (attempts <3)
{
if (usersGuess > random || usersGuess < random)
{
printf ("GUESS AGAIN\n");
printf ("Input guess : ");
scanf("%d",&usersGuess);
attempts++;
}
else
{
attempts = 3;
printf ("YOU WON\n");
}
}

return(0);
}```

Now, the only change i want is
• if the player doesn't guess the right number, then programm print the right number and close and

• if the player guess the right number, then will be asked if he want a new game or not.

Thank you very much.

4. Have a go at your additiomal requirements. You can at least add the lines that output the extra messages.

5. Make up your mind: are you going to write C or C++?
If you are going to write C++, then go re-learn basic I/O.
If not, then this should be moved to the C board.
While you're at it, improving indentation would help a lot.