COMP 2012H
Honors
OOP and Data Structures Lab
3 : Flow Control
and Loops
Number Guessing Game
In this lab, you are going to use C++ to implement a game about
guessing a number between 1 to 100. This is a repeating guessing game that
adjusts the upper and lower boundaries according to the player's guesses to
approach the secret number. The program also keeps a counter on the number of
attempts the player has made.
Program Flow Algorithm:
- First, your program needs to generate a secret number (this part is given
to you).
- Then, the user inputs a number within the lower boundary and the upper
boundary (1 and 100).
- Check whether the input number is within the lower boundary and the upper
boundary.
- If the input number is out of range, output an error statement and ask the
user to input again.
- If the input is within the range, check whether it is the correct answer.
If the input number is not the correct answer, update the new upper boundary
or lower boundary according to the secret number and the input number.
- Loop back to Step 2 until the answer is correct.
- When the user has guessed the number, output a message that the user has
guesses the number correctly and show the number of his/her attempts to guess
the number.
- Ask the user whether he/she wants to play again, if yes (y/Y), loop back
to Step 1.
A suggested skeleton is given here. You can add
anything to the skeleton and design your own style but your program should match
the program flow.
Assessment:
- Does the statement asking for user input display the updated upper and
lower boundaries correctly?
- Can the program check out of range inputs?
- Can the program reach the final result and output the number of attempts?
- Does the program let the user play again by entering either "Y" or 'y'?