Lab4: Guessing game with loops
Contents
Instructions
This lab has two parts:
Part 1: Lab3 Redux
For this part, you will update your Lab3 code so that rather than only give the user 3 guesses, the user has unlimited guesses. Save this lab file as lab4_part1.cpp. The goal is to get the fewest number of guesses.
********************************************************************************
                           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. Try again.
...
Guess 10: 43
That's correct! It took you 10 guesses to find my secret number!
Use a loop to keep prompting the user until they've guessed correctly. Make sure to print out how many guesses it took the user.
Part 2: Menu prompt validation
For part 2, you will create a menu prompt in which you will re-prompt a user for input until it is validated (one of the allowable selections).
Before we begin, let me introduce one common problem with reading in data from
a user: while you might be looking for a string, character, or number, the user
may accidentally enter multiple strings, characters, or numbers. When using
cin, this can cause issues, since cin only reads in
one string, character, or number from the input stream.
Enter cin.ignore(x,y). This is a function (think function like
sin(x) from math class) that will ignore user inputs up to a certain
point. It takes two parameters: x is the maximum number of
characters to ignore in the stream and y a delimiter character. The
function will clear cin either up to x characters or
to the delimiter y, whichever comes first. Here's how it is used:
For part 2, take one of the menu prompts from your PA2.1, copy it
to a file called lab4_part2.cpp. Now modify it to use a loop to validate
the user's input; keep re-prompting the user until valid data is provided.
Be sure to use the cin.ingore(...) function from above to clear
out the input stream after each use of cin.
Submission
Submit your .cpp files to here. You can view the rubric here.
(Back to top)