Need Help for Guess the Number Game

Apr 27, 2013 at 2:27am
I've been working on this "Guess the Number Game" for couple tens of minutes and I need help adding something. I want my program to record how many tries this person has attempted and say how many times he had to try in order to get the correct number. I also want the program to make it the person can only enter in 5 numbers. I would love it if you guys can help me with this! I'm very new at C++ so I'm not very good with the codes,

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
 #include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{

bool goOn=true;
while(goOn)
{
srand(time(0));
int number=rand()%100+1;;
int guess;

char answer;
cout<<"Im thinking of a number between 1-100. Take a guess: ";
cin>>guess; 


while(guess!=number)
{
if(guess>number)
{
cout<<"Too high, guess again: ";
cin>>guess;
} 
if(guess<number)
{
cout<<"Too low, guess again: ";
cin>>guess;
}
}
if(guess==number)
{ 
cout<<"Congrats!! You got it.";
}
   
cout<<"would you like to play again? Enter y or n: ";
cin>>answer;
if(answer!='y')
{
goOn=false;
cout<<"thanks for playing!"<<endl;
} 
}

return 0;
}
Last edited on Apr 27, 2013 at 2:27am
Apr 27, 2013 at 2:47am
You'll want to add a counter to the beginning of the code:

1
2
3
4
5
6
7
8
9
10
int main()
{
srand(time(0));
bool goOn=true;
unsigned int guessCount = 0;   //counter to keep track of attempts.
while(goOn)
{

int number=rand()%100+1;;
int guess;


Then output it at the end:


1
2
3
4
5
6
if(answer == 'n') 
{
   goOn = false;
   cout<<"You guessed "<<guessCount<<" times!"<<endl;
   cout<<"thanks for playing!"<<endl;
}


I'll leave it up to you to figure out where to increment the counter.

there are a couple of things here: I moved your srand() function outside of the loop. seeding the random number generator only needs to happen once, and generally if you seed more then once the outcome tends to be the similar.

The second is that i changed your loop to terminate when the user enters 'n' and not when they don't enter 'y'. The reason is that the game can go on if the user presses anything, but will terminate when entering 'n'. Although its the same kind of idea, it makes more sense from an end user perspective.
Apr 27, 2013 at 3:21am
Hey, thank you for your help, but I'm still having some trouble with the code after I added what you told me to add. Here's the code after I inserted your codes. I got confused when you said I had to figure out where to increment the counter.

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
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{
srand(time(0));
bool goOn=true;
unsigned int guessCount = 0;
while(goOn)
{
    
int number=rand()%100+1;;
int guess;

char answer;
cout<<"Im thinking of a number between 1-100. Take a guess: ";
cin>>guess; 


while(guess!=number)
{
if(guess>number)
{
cout<<"Too high, guess again: ";
cin>>guess;
} 
if(guess<number)
{
cout<<"Too low, guess again: ";
cin>>guess;
}
}
if(guess==number)
{ 
cout<<"Congrats!! You got it. ";
}
   
cout<<"Would you like to play again? Enter y or n: ";
cin>>answer;
if(answer == 'n')
{
    cout << "thanks for playing!" << endl;
    goOn = false;
    cout << "You guessed " << guessCount <<" times!" << endl;
}
}
return 0;
}


And here is the output.


Im thinking of a number between 1-100. Take a guess: 50
Too high, guess again: 40
Too high, guess again: 30
Too high, guess again: 20
Too high, guess again: 10
Too low, guess again: 5
Too low, guess again: 15
Too high, guess again: 13
Congrats!! You got it. Would you like to play again? Enter y or n: n
thanks for playing!
You guessed 0 times!

RUN FINISHED; exit value 0; real time: 12s; user: 0ms; system: 0ms

What I see as the problem is that I want the user to see how many tries he used. Also, as you can see, the last line says I guessed 0 times, which is a problem.
Apr 27, 2013 at 4:10am
You would increment it anytime the user inputs a number as a guess.
Apr 27, 2013 at 4:24am
Oh, ok.
Topic archived. No new replies allowed.