Number Guessing Game using C++

Number Guessing Game using C++ (With Source Code)

Number Guessing Game using C++ (With Source Code)

Hello, coders. Welcome to the codewithrandom blog. In this article, we will learn how to create a Number Guessing Game using C++.

Number Guessing Game using C++

“Guess My Number” game is a simple console-based game implemented in C++ programming language. It’s interface allows the user to guess a randomly generated number between 1 and 100. The user can make as many attempts as they want to guess a number. If the user gets the guessed number right, that is if the the number entered is the same as the number generated randomly by system, then the user wins the game. The game also tells the user, the number of tries made by them to guess the correct number. This game is a basic implementation of an interactive game in C++. It is easy to create an understand.

What is the Guess My Number game?

A Guess My Number is a game in which we use a program that uses the javascript random function to generate one random number between 1 and 0, and the player has to guess the number in fixed attempts. Also, we provide direction to the player on whether the first attempt at selecting a number is greater than the actual number or not. It is simply a hit-and-run game; if the user fails to find the number in a fixed amount of attempts, the game shows you losing, or if you are able to find the number, you win the game.

Number List and Operations Project in C++ (With Source Code)

Features of the game:

  • The game gives the user feedback after each estimate, stating whether the guess was too high or too low.
  • The participant keeps guessing until he or she correctly identifies the goal number.
  • The aim is to predict the number in as few attempts as possible.

Overview of the UI:

Guess My Number Game
Enter a guess between 1 and 100 :
  • The number will be entered by the user
  • If guess>num
Too high!
  • If guess<num

Too low!

  • The number generated is the same as the number guessed
Correct! You got it in ” << tries << ” guesses!
 

Number Guessing Game using C++ Source code:

You can directly use this code by copying it into your IDE to understand the work and then can create it by understanding the project.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    int num, guess, tries = 0;
    srand(time(0)); //seed random number generator
    num = rand() % 100 + 1; // random number between 1 and 100
    cout << "Guess My Number Game\n\n";

    do
    {
        cout << "Enter a guess between 1 and 100 : ";
        cin >> guess;
        tries++;

        if (guess > num)
            cout << "Too high!\n\n";
        else if (guess < num)
            cout << "Too low!\n\n";
        else
            cout << "\nCorrect! You got it in " << tries << " guesses!\n";
    } while (guess != num);

    return 0;
}

Now let us understand the code:-

  • We will start by writing the header of the code with the required libraries – iostream (input and output stream), cstdlib (memory management), ctime (time management).
  • We will write statement – using namespace std; it is used to avoid having to write std:: before standard library functions and objects.
  • Now we will define the main() function. Initializing the variables – num, guess and tries to 0, they are declared to store the target number, the user’s guess, and the number of tries taken, respectively. This where the program will begin taking place.
  • The srand(time(0)) function basically seeds the random number generator along with the current time. This line will confirm that a different sequence of random numbers is generated every time a user runs the program.
  • Now we will generate random numbers between 1 and 100 using the rand() function. These random numbers generated will be stored in the ‘num’ variable. The generation of the rndom number will be done using this expression – rand() % 100 + 1. Breaking down the expression – The ‘%’ operator will be used to get the remainder of dividing rand() function by 100, it will limit the range of the generated number to 0-99. Then we will add 1 to the result shifts the range to 1-100.
  • We will print the heading for the game using cout. The statement is -“Guess My Number Game”.
  • Now we will create a do-while loops which will be used to repeatedly ask the user to guess a number randomly until user make a guess of the correct number. We will use an if-else if-else control statement for the result generation. If the guess is greater than the num, then the system will output “Too high!”, else if the guess is less than the num generated then the system will output “Too low!”, else the number generated is the same as the numbered entered as a guess, then system outputs – “Correct! You got it in ” number of tries “guesses!”. It will state the number of tries, the user made in attempt of guessing the right number.
  • This sums up our project of Creating a Number Guessing Game using C++.

Player Management System using C++ (With Source Code)

Final Output:-

Here is an example to show how this project works.

Number Guessing Game using C++

Conclusion

We have reached the end of this article and have a lot more projects in C++ coming so stay tuned. We have started with awesome and fun projects for you all to understand C++. Learning C++ by creating fun projects makes learning easy and interesting.

Palindrome Checker using Stack in C++ (With Source Code)

If you enjoyed the article and learned something new today, let us know in the comments.

Thank you.

Happy Reading! 🙂

Follow :CodewithRandom

FAQ on Guess My Number Game

What is the Guess My Number game?

A Guess My Number is a game in which we use a program that uses the C++ random function to generate one random number between 1 and 0, and the player has to guess the number in fixed attempts. Also, we provide direction to the player on whether the first attempt at selecting a number is greater than the actual number or not. It is simply a hit-and-run game; if the user fails to find the number in a fixed amount of attempts, the game shows you losing, or if you are able to find the number, you win the game.

What is the Advantage of Guess My Number Game?

This type of game helps increase the guessing and thinking power of the player and helps enhance critical thinking. Also, working on this type of project helps developers get real-time experience with C++ concepts.



Leave a Reply