Design and Code Perform Guess
Now that we have the different functions and procedure, we can now start to design the control flow for the Guess that Number program. For the Guess that Number program we can start by designing the control flow within the Perform Guess
function. The specification of this is shown the table below. Think about the steps that need to be performed to achieve this. If you had been asked to do this what would you need to do?
Function | Perform Guess |
Description | Perform Guess is responsible for coordinating the actions needed to perform a single guess within a game of Guess that Number. The user’s guess is read, and the value checked against the target value. A message is then output telling the user if the target value is less than, larger than, or equal to their guess. This function returns true when the user’s guess is equal to the target, otherwise it returns false. |
Returns | Boolean : True when the user has guessed the number, false otherwise. |
Parameters | Guess Number : The number of the current guess (from 1st to 7th guess), used in the prompt asking for the user to enter their guess. |
Target : The number the user is aiming to guess. |
Read the Guess
The first task the function needs to perform is to get the guess from the user. This can be performed in a sequence: display a prompt, read the value from the user.
This sequence sounds familiar. Rather than re-coding this here, we can incorporate our read_integer
function in the program that we created earlier.
Let’s start coding this up. Create a new C++ code file, and we can put in place some starter code and then build out from this iteratively. We can use the following as a starting point. Add in your read integer and build the start of perform guess.
Give Feedback
The next step in this sequence is to give the user feedback based upon their guess and the target number. This code requires the ability to select a given branch. The computer needs to output different messages based upon the user’s guess.
We can group together a few if-else statements to achieve this. The first if-else
block can check if the target
is greater than the user’s guess
. If this is true then the computer can take the first branch and output the message ‘The number is larger than’ and the value from the user’s guess.
Within the false path from the first decision we can include another if-else
block to check if the target is less than the user’s guess. When this path is taken, we can get the computer to output the message ‘The number is less than’ and the value from the user’s guess
Finally, we have the remaining else
path. This is taken when the guess it is not larger or smaller than the target, meaning this path is taken when the guess is equal to the target. This can output a success message.
These three paths are illustrated in the following flowchart.
Have a go at implementing this before we look at what value to return.
When you run this make sure to test that each branch is run correctly.
Returning
The last action in the code is to return a Boolean result indicating if the user’s guess is equal to the target number. This may sound like a place where we need to use an if statement - i.e. if the target equals the guess, return true, else return false. This would work, but we can do this more efficiently and concisely.
The flowchart below shows the trick that is being performed at the end of perform guess
’s code.
Perform guess needs to return a result indicating if the user has guessed the number of not.
This will be a Boolean value, with true indicating the user guessed the number.
Instead of using an if statement, you can just directly return the expression that would have been used in the if statement’s condition. Think about it. If the condition was true, you returned true, if the condition was false you returned false. It is much easier and faster to just return the value of the condition itself.
Have a go at adjusting this in your perform guess function. Adjust main so that you can check what the result is. Here is what I used to test this.
Test it out
What happens if you enter a value larger than 100 or less than 0? Could we use anything we have created before to give a better user experience in these cases? They are not really valid guesses.