Introduction
In the previous tutorial, we learned how to read in numbers from the user. In this tutorial, we will learn how to make decisions based on data that you have stored in variables. This will allow us to create the childhood game of guess what number I’m thinking of.
Code
So, the way that we make decisions is through the if statement. Its usage looks like this:
if (x == 5)
{
cout << "X equals 5" << endl;
}
else
{
cout << "X is not equal to 5" << endl;
}
The code works exactly how it reads, if x equals five then print the message in the first set of curly braces, if not, print the message in the second set of curly braces. The one thing to note is that the single equals sign (=) means assignment, as in setting a variable to a value, and the double equals sign (==) is the equality test, as in are the two things equal. They are very similar, but mean very different means.
The next thing that we need to do is choose a random number, because if we picked five every time, the game wouldn’t be very much fun at all. To do this, we use the rand() function from the cstdlib library. Remember how to include a library from the first tutorial? Here’s the code to generate a random number and write it to the screen:
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int num = rand();
cout << num << endl;
return 0;
}
If you run the following program a couple of times, you might notice that the value is always the same. Well, that’s not very random. This is because computers are deterministic, so the values aren’t really random, but they are usually close enough. The random number generation algorithm works by using a seed and then generating random values based on that seed. So, given a different seed, the random number generator will produce a different value. I know what you are thinking: “So to get a random number out of the computer, we need to give it a random number? Well, if I had a random number, I wouldn’t need one.” Fortunately, there is a standard trick to get around this. Typically, the random number generator is seeded with the current time. Using the current time as the seed, you will get a different value every time you run the program. To get the current time, use the time() function in the ctime library and the seed function for the random number generator is srand(). The updated program looks something like this:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
srand((unsigned int) time(NULL));
int num = rand();
cout << num << endl;
return 0;
}
This should now generate a different number every time you run the program. However, you might notice that some of the numbers generated are rather large. To make the game reasonable, we really only want a random number in the range of 1 to 10. One way to do this is to take the remainder of the random number when divided by 10. There is a built in operation that lets us do that and it is called the modulus operator and uses the percent symbol (%). Now if we take the remainder when we divide the number by 10, that will give us a number between 0 and 9, so we just add one to get a number between 1 and 10. The code looks like this:
int num = (rand() % 10) + 1;
So, that should be everything that we need to generate our guess a number game. Give it a shot and see how it turns out? Can you make it so that you have three chances to get the number correct? As an added bonus, can you tell the user whether or not the secret number is higher or lower than the guess? There are less than (<) and greater than (>) operators that work similarly to the equality (==) operator. One way to do it is attached below, but you should try and see what you come up with first.
Summary
In this tutorial, we discussed how to make decisions based on user input using the if statement. Armed with this knowledge and a random number generator, we made a guess the secret number game.
When you were coding up the game and giving the user three guesses to get the number correct, did you notice that there was a lot of logic that was duplicated? (Or if you looked at the attached code, did you notice that?) In the next tutorial, we will discuss for loops which let you do something many times in a row.
If you have any questions or comments about what was covered here, post them to the comments. I watch them closely and will respond and try to help you out.