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:

  1. First, your program needs to generate a secret number (this part is given to you).
  2. Then, the user inputs a number within the lower boundary and the upper boundary (1 and 100).
  3. Check whether the input number is within the lower boundary and the upper boundary.
  4. If the input number is out of range, output an error statement and ask the user to input again.
  5. 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.
  6. Loop back to Step 2 until the answer is correct.
  7. 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.
  8. 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: