Guessing game

Let us consider a guessing game in which the computer picks a random twodigit number in the range from 0 to 99 and asks the user to guess the number. After each
guess, the computer lets the player know how many digits of guessed numbers match the
correct number. If the guessed number matches the correct number, the computer lets the
player know the number of guesses the player made.
1. The program accepts only the positive numbers, i.e., the acceptable range of numbers
is 0 to 99 inclusive.
2. The player tries to guess the number computer picked.
3. If the player enters an incorrect guess, the computer lets the player know how many
digits from the guess are also in the correct number. The order of digits doesn’t affect
the number of digits that match. For example, if the correct number is 21 and the
player guessed 1, the computer reports one digit because the player’s guess contains 1.
If the guess contains two digits that match, the computer reports two digits. The user
still needs to come up with the correct order of those digits to arrive at the correct
number.
4. The program should be robust against both incorrect numbers and non-numeric character (tokens) input.
5. The program must keep count of number of guesses.
6. When user enters the correct guess, the program reports the number of guesses the
player made.
7. The program should contain the following functions.
• Write a function to print the introduction to the game. The call to this function
should be the first line of your main() function. This function does not take any
arguments and does not return a value. The computer is a player here. It tells
the user to guess a number from 0 to 99 and it will tell how many of those digits
from the guess appear in its secret number.
• Write a function called by main() named digitMatches that takes two arguments,
the number computer generated and the player’s guess, both of type int and
returns an integer, the number of matching digits. It doesn’t display the matching
digits, it just lets the player know how many digits match.
• Write a function called by main() named guessNumber that does not take any
arguments but returns a number in the range from 0 to 99. This function validates
the range of the guessed number. This function is repeatedly called until the guess
is within the range from 0-99. This function calls the following function to get an
integer value.
• Write a function called by guessNumber() named getInt that takes one argument
of type string. It is a prompt to get number from the user. This function returns
an integer. This function handles the nonnumerical input.
1
• Finally write the main() function where the game is played. The main Creates the
random number from 0 to 99. guess outside the loop. It calls the guessNumber()
function for the number guessed and stores it in an int variable called guess. The
loop runs as long as the guess is not equal to the computer generated random
number.
8. Each function should be tested as soon as it is written. To do that, write the introduction and write the main() function and call the intro function.
9. To test the digitMatches() function, just call only that function with different inputs
by giving two numbers. The function must work for any number in the range from 0
to 99.
10. You test the getInt() for non-numeric input first and then the guessNumber() function
for the value of the number in the correct range.
11. The goal of testing is to make the function fail as soon as possible so that, you can
correct it immediately. Please do not use strings int his program. You should
be able to do digit matches perfectly without using a string.

The following is the example log of the execution:
Try to guess my number form 0 - 99 and
I will tell you how many digits of your
guess match my number.
Your guess? 50
Incorrect (hint: 0 digits match)
your guess? 73
Incorrect (hint: 0 digits match)
Your guess? yes
That’s not an integer. Try again
Your guess? 79
Incorrect (hint: 0 digits match)
Your guess? 300
Invalid number. Try again!
Your guess? 34
Incorrect (hint: one digit matches)
Your guess? 36
Incorrect (hint: 0 digits match)
Your guess? 48
Incorrect (hint: 2 digits match)
Your guess? 84
Congratulations! You took 9 guesses.



Where do I start?
Read all the instructions.
8. Each function should be tested as soon as it is written. To do that, write the introduction and write the main() function and call the intro function.
9. To test the digitMatches() function, just call only that function with different inputs
by giving two numbers. The function must work for any number in the range from 0
to 99.
10. You test the getInt() for non-numeric input first and then the guessNumber() function
for the value of the number in the correct range.
11. The goal of testing is to make the function fail as soon as possible so that, you can
correct it immediately. Please do not use strings int his program. You should
be able to do digit matches perfectly without using a string.

It tells you exactly how to proceed.
Read all the instructions.
Put in some effort
Don't sleep through lectures
Get out of Bed
Don't use oxygen if it is more important for others.
A 4 word question is pathetic
Hello faizankhan40,

As salem c and the TheIdeasMan say read all your instructions. From 1,2 and 3 I sees the variables "computerGuess" and "userGuess". #5 would suggest "numOfGuesses". And do not be afraid of long variable names.

Then I would look at #7 and start with "main" and the introduction. That is the easy part. Write the function compile and test it.

At this point I would be think about either using "rand()" or the RNG from the "random" header file. I do not know what you have learned or what you can use.

Next I would go back to #4 and write a function that does what you need and returns a good number.

There is a start and something to think about.

Andy
Topic archived. No new replies allowed.