Lab3: Guessing game
Contents
Instructions
In this lab, you will write a program that randomly picks a number between 1 and 50. The user then gets three guesses. If at any point the user guesses the randomly picked number, the user wins. Otherwise, if the user's three turns are up, they've lost.
This program should look something like this (my input is in this color):
******************************************************************************** GUESS A NUMBER ******************************************************************************** I'm thinking of a number...okay, got it. I'll give you three guesses as to what number I have in mind. It's between 1 and 50. Guess 1: 16 That's too low. Try again. Guess 2: 35 That's too low. Try again. Guess 3: 46 That's too high. That's all your guesses! You LOOSE.
You can use this example as your script, or you can do something similar. The important parts are, the program must:
- randomly pick a number
- put
#include <stdlib.h>
at the top of your file - put
#include <time.h>
at the top of your file - put
srand(time(0))
at the top of main, but below your variable declarations - assign a random number to a variable like this (supposing your
variable is called
secretNumber
):secretNumber = rand() % 50 + 1
- put
- the user gets three guesses
- for each guess, tell the user if it's too high, too low, or correct
- if correct, tell the user they won
- if incorrect, after the first two guesses, tell the user to guess again
- if incorrect after the third time, tell the user they've lost
Here is a C++ template to get you started. Copy and paste this into a Sublime Text editor and begin writing your code. Don't forget to add in the include statement mentioned above. You should use an incremental approach: implement one or two lines, then try compiling and test the program.
Submission
Submit .cpp file to here. You can view the rubric here.
(Back to top)