CommonLounge Archive

Exercise: Hotter or Colder

September 11, 2018

In this exercise, you will be implementing a game known as Hotter or Colder. This exercise will (a) give you a chance to practice the skills you have been learning, such as variables, if-statements, loops, etc and (b) teach you about generating random numbers in C++. Also, by the end of it, you will have a really fun game to play when you’re bored!

If you don’t have C++ installed on your computer, you can do your work here: C++ Shell. Make sure you keep saving your work! Or, you can see C++ installation instructions here.

Overview

Here’s how the game works.

  1. The program chooses a secret number at random between 1 and 100, and the user is asked to guess it.
  2. If the guess is too low, the program should output “You’re too cold”. If the guess is too high, it outputs “You’re too hot”.
  3. This repeats till the user guesses the secret number.
  4. When this happens, the program outputs, “Congratulations, you did it!” and exits.

Here’s a sample interaction with the game:

Secret number chosen (between 1 and 100).
Start guessing! ... 
70
You're too cold!
85
You're too cold!
95
You're too hot!
90
You're too cold!
93
Congratulations, you did it! 
Secret number is 93

Step-by-step guidance

Below, we have provided step-by-step guidance for you to implement the above game. If you would like to implement this exercise without any help, the only section you should look at is ”Step 1: Generating random numbers”, since you know everything else.

Step 0: Boilerplate code

Start with the boilerplate code you have been using so far in the course.

Step 1: Generating random numbers

You can use the following code snippet to generate a random number between 1 and 100.

srand(time(0));
int a = (rand() % 100) + 1; 
cout << a << endl;

If you’d like to understand how random number generation works in C++, see the appendix.

Step 2: If statements (single guess)

Write the if … else if … else statements so that your program outputs

  • “You’re too cold” if the guess is less than the secret number
  • “You’re too hot” if the guess is more than the secret number
  • “Congratulations, you did it!” if the guess is equal to the secret number

Some tips:

  • Output the secret number right after generating it. This will help you make sure your program is working correctly. Once we’re sure everything is working properly, you can remove that line.

Step 3: Make it a loop.

Now, move the if … else if … else statements inside a loop, so that the user can make multiple attempts at guessing the number.

Hint:

There are multiple ways to do this, but the easiest solution uses an infinite while loop, with a break when guess == secret_number.

Congratulations

Congratulations, you have made your first game in C++. Keep it up!

Solution

Here’s the official solution to this exercise: C++ Solution: Hotter or Colder.

Appendix: Understanding random numbers in C++

C++ has a special function called rand() which generates a random number.

int a = rand();
cout << a << endl;

There are two problems with the above:

  1. rand() generates any random number between 0 to RAND_MAX, where RAND_MAX is it’s max limit. (not necessarily between 1 and 100).
  2. Every time you run the program, it will generate the same random number! So if you run the program 5 times, it will always generate the same number.

The solution to the first problem involves using the % operator.

int a = (rand() % 100) + 1;
cout << a << endl;

rand() % 100 is always between 0 and 99. Then, we add 1 to make sure the result is between 1 and 100.


For the second problem, we will use srand() function along with rand(). A random number generator (rand()) accepts something called a seed. If the random number generator is given the same seed, then it will generate the same sequence of random numbers. Be default, the seed is 0.

We can take an example to understand this more clearly. Let’s create a sequence of random numbers with a for loop.

// This program will create same sequence of
// random numbers on every program run 
#include <iostream>
using namespace std;
int main() {
    for (int i = 0; i < 5; i++)
        cout << rand() << " ";
    return 0;
}

This program will create same sequence of random numbers on every program run. For example:

Output 1:

1804289383 846930886 1681692777 1714636915 1957747793

Output 2:

1804289383 846930886 1681692777 1714636915 1957747793

Output n:

1804289383 846930886 1681692777 1714636915 1957747793

That is, multiple calls to rand() produces a sequence of random numbers. But if we run the program multiple times, the same sequence of random numbers is produced.

The srand() function is used to change the seed of the random number generator. By setting srand(time(0)), you are setting the seed of the random number generator to the current time. Hence, every time you run the program you will get different random sequences:-

// This program will create different sequences of 
// random numbers on every program run 
#include <iostream>
using namespace std;
int main() {
    // set the seed of the random number generator to the current time
    srand(time(0)); 
    for (int i = 0; i < 5; i++)
        cout << rand() << " ";
    return 0;
}

Output 1:

765731694 1526463226 1982858079 1866096666 1911076665

Output 2:

662066188 1385309989 1866855391 283261215 1506197291  

Hence, we use the following code to generate a random number between 1 and 100:

srand(time(0));
int a = (rand() % 100) + 1; 
cout << a << endl;

© 2016-2022. All rights reserved.