Building a Number Guessing Game in C++

In this C++ tutorial, we are going to learn how to make a number guessing game in C++. This particular game is called number guessing game in which you have to guess the number and you will be given a certain number of chances. If you guess the right number you will win otherwise you will lose. This game is also known as guess the number game in C++. Let’s begin our tutorial.

Algorithm:

  • First of all, we will generate a random number using rand() function in C++.
  • Then we will take input from the user if input matches to the random number, the user will win and the game will terminate.
  • Otherwise, there will be a certain number of chances until which the game will run. If the user still wasn’t able to get the right guess he/she will lose.
  • One can use his own logic regarding the range of number and number of turns.

Number Guessing Game in C++

Source code in “.cpp” file:

// Number guessing game in C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i;          
    int number; 
  
  // Generating different random numbers each time game will run         
    srand(time(0));
  
  //Generating random numbers   
    number=rand()%51;
    cout<<"Guess a number between 1 and 50, only 5 turns will be allowed: ";
    for(i=0;i<5;i++)
    {
    cin>>n;
    if(number==n)
    {
      cout<<"\nCongrats, you have guessed the right number which is, "<<number;
      cout<<"\nNumber of turns you have used: "<<i+1;
      break;
    }
    else if(number>n)
    {
      cout<<"\nYour guess is lower than right number, Turns left: "<<5-(i+1);
      if(5-(i+1)>0)
      {
          cout<<"\nTry again, entering a number higher than previous: ";
        }
    }
    else if(number<n)
    {
      cout<<"\nYour guess is higher than right number, Turns left: "<<5-(i+1);
      if(5-(i+1)>0)
      {
          cout<<"\nTry again, entering a number lower than previous: ";
        }
    }
  }
  if(i==5)
  {
    cout<<"\n\nSorry, you failed to guess the right number.";
    cout<<"\nThe right answer is: "<<number;
  }
    return 0;
}

Output:

Guess a number between 1 and 50, only 5 turns will be allowed: 30

Your guess is higher than right number, Turns left: 4
Try again, entering a number lower than previous: 20

Your guess is lower than right number, Turns left: 3
Try again, entering a number higher than previous: 25

Your guess is lower than right number, Turns left: 2
Try again, entering a number higher than previous: 28

Congrats, you have guessed the right number which is, 28
Number of turns you have used: 4
--------------------------------
Process exited after 31.05 seconds with return value 0
Press any key to continue . . .

Key points of this Game:

  1. rand() function is used to generate random numbers.
  2. srand() function is used to generate different random numbers each time game will run.

You can also check out this game in other programming languages,

Build a Number Guessing Game in Python

Guess The Number Game Using Java with Source Code

Guess The Number Game Using JavaScript

Do not forget to comment if you want to learn more about this post or you want to share some information regarding the same.

Also learn:

Leave a Reply

Your email address will not be published. Required fields are marked *