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.