Guessing game in C++

In this tutorial we will create a guessing game. The computer will generate a random number between 1 to 100, ask the player to guess a number within that range and then tell the player if the number is higher, lower or correct. Here is the full source code:

#include <iostream>
#include <time.h>

int main() {
   srand(time(0));
   int number = rand() % 100 + 1;
   int guess;

   std::cout << "Welcome! Guess a number between 1 - 100: ";
   std::cin >> guess;

   while (guess != number) {
      if (guess < number) {
         std::cout << "Higher, guess again: ";
      } else {
         std::cout << "Lower, guess again: ";
      }
      std::cin >> guess;
   }

   std::cout << "Correct! The number was " << number << std::endl;
   return 0;
}

 

Faking the random

Since there is no such thing as true random in this world, we need to fake this somehow. Pay attention to the first line in the main function:

srand(time(0));

The function srand(unsigned seed) takes a seed as a parameter. This seed is used to generate a unique number next time we call rand(), which we do in the very next line. If we use a seed that isn't time related, say seed(1) (which is the default parameter), and generate a sequence of random numbers, we sould see the same sequence of numbers over and over again. This is why the time comes in handy. Time is constantly changing, therefore the seed will always be different, which will make the value different everytime we call rand().

 

Random interval

In the next line the number that is about to be guessed is generated:

int number = rand() % 100 + 1;

A number is generated by int number = rand(), then the interval is set by % 100 + 1. Why the "+ 1"? You ask. This is needed because int number = rand() % 100 will generate a number between 0 - 99. So we add one to get the desired interval 1 - 100.

 

Stuck in a loop

Next we declare an int where the guesses will be held, then we just welcome the player and them to make a guess, and we store that guess in the integer we just declared so we can compare it with the computers number. The first comparison is done in a while-loop:

while (guess != numnber) {

This line tells the computer to run the code inside the while-block while the guess is not equal to the computers number. The first thing we check if this condition is true is if our guess is less than the computers number. If it is, we want to know. If it it is higher, we also want to know. After that, we want to be able to make a new sophisticated guess with our new knowledge about the number:

while (guess != number) {
   if (guess < number) {
      std::cout << "Higher, guess again: ";
   } else {
      std::cout << "Lower, guess again: ";
   }
   std::cin >> guess;
}

And that's about it. When the player guess the correct number, he is free from the loop and told that the number was correct. The output will be something like this:

Welcome! Guess a number: 50
Higher, guess again: 75
Lower, guess again: 63
Higher, guess again: 69
Correct! The number was 59

 

 

 

Enjoyed this article? Give the teacher an apple.

cookie

1

Author

authors profile photo

Articles with similar tags

Comments