Number Guessing Game in C++

Written By: Jeremy

- 01 Jun 2006 -
















Description: In this guide I will use loops and the random number generator to build a very basic game. The article generally helps the programmer in applying the srand() function and loops in C++.

  1. Number Guessing Game with C++

Introduction:

This guide is aimed at the beginning programmer, assuming he/she is past 'Hello World' and then some. The programmer will need to know about basic C++ structure along with other basic background information on the language.

Game Introduction:

The game consists of an input (the user's guess) compared with the random number that the srand() function generates. Should the guess be smaller or larger than the generated number, the program will tell the user if the guess is too low or too high and ask for another input. Upon finally reaching the program's generated number, the program will congratulate the user and close.

Getting Started:

I am using Bloodshed Dev-C++ 5 Beta for this tutorial. Throughout the article I'm going to assume that you are using an IDE.

Let's Code!:

First we will need to start off our program with the headers and a few comments. Below I've typed out the code for you to either copy and paste or type out in your IDE of choice.

// Number Guessing Game
// Applies the srand() function and loops.
 
#include 
#include 
#include 
 
using namespace std;

First of all, we need to keep in mind that the random number function will not produce a random number each time. It is infact a long sequence of different numbers. What this means is that once the generator is done producing the sequence of what would seem random numbers, it will repeat the sequence with the exact order of the numbers. However, we can further randomize this function by having the program retrieve current time variables which will be plugged into the program's formula for producing these 'random numbers.'

The cstdlib header is included as this will generate the random number. After, the ctime header is included so that we can seed the random number generator with the current time.

In this next piece of code we seed the random number generator.

int main()
{
        srand(time(0));
        int randomNumber = rand() % 50 + 1; // generates random number between 1 and 50
        int guess = 0;
        
        cout << "\tThe Number Guessing Game\n\n"; // Title of program. 

In the above code we can replace '50 + 1' with '100 + 1' (generates random number between 1 and 100), '1000 + 1', etc. If you would like, you can apply a switch statement and have different modes of difficulty. For example, case 1 (easy) could be '25 + 1', case 2 (medium) could be '100 + 1', and case 3 (hard) could be '1000 + 1'.

This next piece of code uses a loop.

 do
        {
                cout << "Enter your guess (#1-50): ";
                cin >> guess; // put variable into 'guess'
 
                if (guess < randomNumber) // If guess is < than the random number, display
                        cout << "Your guess was too low\n\n"; 
 
                if (guess > randomNumber) // If guess is > than the random number, display
                        cout << "Your guess was too high\n\n";
 
        } while (guess != randomNumber); // While guess is not equal to the random number
 
        cout << "\nCongratulations! You've guessed the number.\n\n";
        system("pause");
        
        return 0;
}

And that's it! We're done with the program. All that's left now is to test and customize.