Simple guess number game

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 (in 3 tries), then the programm will close
if the player guess the right number then print "you won" 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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#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.
Last edited on
This will not produce a random number every time, it will only make a random number and every time you open the program, it will have the same number, which isn't really random. The only way to make it random every time is to use the <time.h> header, with an initialization of the random code.

Use this under where you decalre variables:

srand(time(0));

then use this random number function:

random = (rand() % 20 +1)

This will give you a random number from 1 to 20 every time. Just pu the function into your while and it will make a random number.
Last edited on
Oh, yes, you have right. But this is not problem for me now.

Now, i want change to this:
-if the player doesn't guess the right number, then the programm will 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.

If you can help, you are welcome!
Not sure how to use the stdio, but it would be somethin like this
1
2
3
4
5
6
7
8
9
10
11
if (random == usersGuess)
{
      printf("%d was the right guess! Would you like to play again(0 for no, 1 for yes)?", usersGuess)
      scanf(usersGuess);//Then have your while be (usersGuess == 1)
}

else
{
    printf("That was not the right number. The correct number was %d", random);
    return 0;
}


I think that is what you wanted
Thank for the reply.
I don't want exact this.

The <<"printf("That was not the right number. The correct number was %d", random); ">> must printed if the player try 3 times.

Also, i want the programm finish if the player give "no" at question.


I have confused here!

How can i succed this?
Last edited on
well make a variable called

int counter = o;

then do: counter++; every time you go through the failure. Then if (counter == 3) do a return 0;

hopefully that is what you mean
First thing I would do is, if you really want to be able to repeat the whole set of instructions over and over...take it out of main. I'm not saying you have to, but it makes it easier for me personally to digest.

And then, as Skwylar said, make a loop that runs three times, and break the loop with a correct guess, print the guess and ask if the player wants to play again. If the player goes three times, and does not succeed, then just allow the function to end, and the program will reach it's natural conclusion. (I'm not really a fan of returns in the middle of a function. There are other ways to do that, and less potentially disastrous.)
Topic archived. No new replies allowed.