Random Number generator

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 . . .
use code tags and indent your code, please
I deleted for loop and initialised played to 0.Then,in the while loop its incremented as required.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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;
}
Last edited on
Topic archived. No new replies allowed.