C Programming Tutorial 50 – If Statement Guessing Game

In the last blog I spoke of Relational Operators. Check the one out first! Are you new here? Start at the beginning, Intro to C!

In this blog we are going to be creating an if statement with relational operators.

We want to create an application that gives the user one try to guess a number between 1 and 5. As we get more advanced, we can do things such as give them multiple tries and see how many times it takes them to get it right. For now, we are just getting the very basics.

Some of the code in here is going to be a bit ahead of where we’ve been studying, so don’t freak out if you don’t grasp it all. Just trust me that it works and type it down if you don’t understand.

First, we are going to make the software store a number to be guessed. To do this, we want the computer to generate a random number. Now, it’s actually surprisingly hard to generate a random number, so often times computers use what is known as a psuedo-random number generator.  Pseudo means not genuine, but it is the closest thing we can get to a random number.

A pseudo random number generator always needs to be what is known as seeded. Basically, the random number has to be based off of some value, which is called the seed.  Usually random numbers are generated by the computer’s clock because they change so rapidly.

I’m going to teach you how to make a random number generator based on the system clock, but please know that this is not the most secure or best random number generator ever. In this situation it works fine, but you don’t want to use this method for like a super serious password or something.

First, we must seed the random number by calling srand and passing in the time:

Don’t worry if you don’t understand everything that is going on here.  Basically, we are calling a function that gets the time, and we are passing that time to the srand function. NULL is there because the time() requires us to pass in a pointer, and we are saying we don’t have a pointer for you.

Next, we get a random number: 

This will get us a number, but the number might be too big, so what we can do is actually divide this number by 5 and get the remainder, which will give us a value between 0 and 5: 

This is not a full proof method because it will actually cause certain numbers between 0 and 5 to be picked more than others, but it really doesn’t matter in this situation.   

So all together, your code looks like this so far:

Next, we have to create an integer variable to store the user input.

Remember that we need to give the address of the variable using the address-of operator. 

next, write the if:

Run the program and see how it works.   

Now, the problem we have here is that if the user guesses correctly,  it will print you win and then you fail. There is an easy way to fix this. Put return 0 in the if statement like this:

Run the program and see that we fixed the problem.   

Remember that return 0 is how we end the program, so we are just ending the program early if the guess is correct. This is a perfectly valid way to do what we are trying to accomplish, but there is a way that is probably a bit better. Think about different ways we could do what we are trying to do and see what you come up with. In the next blog I’ll be giving you a solution.

Leave a Reply

Your email address will not be published. Required fields are marked *