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 "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!

#include <stdio.h>
#include <stdlib.h>


main()


{     


      int x;
      int guess; 
      int tries;
      char answer;
      
      do
      x = rand();
      while ( x < 0 || x > 20 );


      tries=0;


do      
      do


        do
        printf("Put your guess from 0 to 20 please;");
        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?");
           scanf("%s", answer);
        else 
            printf ("wrong number!");
  
      while tries=3


while (answer!=no);


}

Thank you very much.

Try formating the code and start using braces "{}"

And post problems you are having. Don't make us guess.

Try formating the code and start using braces "{}"

And post problems you are having. Don't make us guess.

I'm sorry. You have right.

I wrote new 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.

bool playAgain = true; //control outer loop
bool notFound = true; //control inner loop
int attempts = 0; //control inner loop
char again;

while(playAgain)
{
 while (attempts less than 3 and notFound)   
 { 
  printf ("Input guess : ");     
  scanf("%d", &usersGuess);      
  
  if (usersGuess > random || usersGuess < random)         
     attempts++;     
  else if (usersGuess == random)         
     notFound = false;   
 }

 //find out why inner loop stopped and print appropriate output
 if(attempts == 3)
 {
  printf("Sorry, you lost.  ");
  printf("The number was: ");
  printf(random);
 }
 else
  printf ("YOU WON\n"); 


 //check if player wants to play again
 printf("play again, y/n");
 scanf(&again);

 if(again == 'y' || again == 'Y')
 {
   //reset notFound and attempts to default values here
 } 
 else
  playAgain = false;
}

Trying to explain it is harder than doing it. Basically there are two loops. The outer controls how many times to play the game. The inner controls the game. The game continues as long as player hasn't guessed 3 times or until player guessed correct answer. My C is rusty so I won't guarantee appropriate I/O syntax, but the logic should be solid.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.