7. Random Number Guessing Game (FOR EXTRA CREDIT – 5 points ONLINE SECTION ONLY)
Write a program that generates a random number between 1 and 15 and asks the user to guess what the number is. If
the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess
is lower than the random number, the program should display “Too low, Try again.” The program should use a loop that
repeats while keeping a count of the number of guesses the user makes until the user correctly guesses the random
number. Then the program should display the number of guesses along with the following message “Congratulations.
You figured out my number.” Suggest that you also give the user the opportunity to play the game again or quit.
See sample “career_predictor.cpp” program for set-up and implementation of random numbers in your program above.
// what variable do i use to count how many times number != randomNum ??
// i just keep getting a one.
// Extra Credit
//Random Number Guessing Game
#include <iostream>
#include <cstdlib>
#include<ctime>
using namespace std;
int main()
{
int number;
int randomNum;
bool tryAgain= true;
char reply;
int guess;
cout <<"Want to play a game?"<<endl;
cout <<"Try and guess the number I am thinking of"<<endl;
cout <<"and you will win."<<endl;
while (tryAgain)
{
srand(time(0));
randomNum = rand() % 15 + 1;
cout <<"I'm thinking of a number between 1 and 15."<<endl;
cout << "\nTry and guess my number: "<<endl;
cin>>number;
// what variable do i use to count how many times number != randomNum ??
// i just keep getting a one.
for (int played = 1; played == (number!= randomNum) ; played++)
{
while (number!= randomNum)
{
if (number < randomNum)
{
cout << "Your guess is too low...try again: "<<endl;
cin>>number;
}
if (number > randomNum)
{
cout << "Your guess is too high...try again: "<<endl;
cin>>number;
}
}
if (number == randomNum)
{
cout << "Congratulations you have won."<<endl;
cout << "You made "<<played<<" failed attempts."<<endl;
}
}
cout<<"Dare to try again? Enter y or n: "<<endl;
cin>>reply;
if(reply!='y')
{
tryAgain=false;
cout<<"Quitter."<<endl;
}
}
system("PAUSE");
return 0;
}
[/code]
OUTPUT
Want to play a game?
Try and guess the number I am thinking of
and you will win.
I'm thinking of a number between 1 and 15.
Try and guess my number:
5
Your guess is too high...try again:
4
Congratulations you have won.
You made 1 failed attempts.
Dare to try again? Enter y or n:
y
I'm thinking of a number between 1 and 15.
Try and guess my number:
6
Your guess is too high...try again:
5
Your guess is too high...try again:
4
Your guess is too high...try again:
3
Congratulations you have won.
You made 1 failed attempts.
Dare to try again? Enter y or n:
y
I'm thinking of a number between 1 and 15.
Try and guess my number:
4
Your guess is too low...try again:
5
Your guess is too low...try again:
6
Congratulations you have won.
You made 1 failed attempts.
Dare to try again? Enter y or n:
n
Quitter.
Press any key to continue . . .
int main()
{
int number;
int randomNum;
bool tryAgain= true;
char reply;
int guess;
cout <<"Want to play a game?"<<endl;
cout <<"Try and guess the number I am thinking of"<<endl;
cout <<"and you will win."<<endl;
while (tryAgain)
{
srand(time(0));
randomNum = rand() % 15 + 1;
cout <<"I'm thinking of a number between 1 and 15."<<endl;
cout << "\nTry and guess my number: "<<endl;
cin>>number;
// what variable do i use to count how many times number != randomNum ??
// i just keep getting a one.
int played=0;while (number!= randomNum)
{
played++;if (number < randomNum)
{
cout << "Your guess is too low...try again: "<<endl;
cin>>number;
}
if (number > randomNum)
{
cout << "Your guess is too high...try again: "<<endl;
cin>>number;
}
}
if (number == randomNum)
{
cout << "Congratulations you have won."<<endl;
cout << "You made "<<played<<" failed attempts."<<endl;
break; // finish and ask for y or n
}
}
cout<<"Dare to try again? Enter y or n: "<<endl;
cin>>reply;
if(reply!='y')
{
tryAgain=false;
cout<<"Quitter."<<endl;
}
system("PAUSE");
return 0;
}