Number guessing game help(Arrays)

Pages: 12
Hello, I am trying to keep track of the number of guess each time then display the lowest score(guesses). Can anyone help, and suggest where I should implement the code to do so? Thanks
<
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int numberofguesses=0;
bool playagain=true;
char answer;
int guesses;
int num;
int guess;
bool isGuessed;
srand(time(0));
num=rand()%1000;
isGuessed=false;
while(!isGuessed, playagain=true){

cout<<"Enter an integer greater than or equal to 0 and less than 1000"<<endl;
cin>>guess;
numberofguesses++;
if(guess>1000 or guess<=-1)
{
cout<<"Please enter proper number, your number is out of range"<<endl;
break;
}

if(guess==num)
{
cout<<"You guessed the correct number."<<endl;
isGuessed=true;
cout<<"It took you "<<numberofguesses<<" "<<" tries to guess the correct number!"<<endl;
cout<<"Would you like to play again? Enter Y/N"<<endl;
cin>>answer;
if(answer='N')
{

cout<<"Thanks for playing!"<<endl;

}
else if(answer='Y')
{
playagain=false;
}

}

else if (guess<num)
cout<< "Your guess is lower than the number. Try again."<<endl;

else
cout<< "Your guess is higher than the number. Guess again!"<<endl;

}
}
>
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/178365/

Don't double post please
Last edited on
Just need a little loving :)
closed account (48T7M4Gy)
Just need a little loving :)

That may be true but I count at least three separate posts by you on the same question. The trouble with that is it wastes time in all the duplicated effort and prevents you from getting a comprehensive response. Play fair please. I'll have a look at this one and my response will depend on you posting a suitable cancellation post on the others you have made.

BTW you need to use code tags <> via the toolbox on the right.
Last edited on
closed account (48T7M4Gy)
Part 1:

keep track of the number of guess each time

use an int counter = 0 at the start of each game which is incremented by 1 on each new guess.

Sweet! I do have that already :D"numberofguesses++" then in the if statement it is displayed. I guess to clear up, I want to keep track of the number of guesses each time say the first time the program is run the person guesses 12 times, the next time(saying yes to the question) the person guesses in 10 tries. Then a cout statement would say "your low score is 10, congratulations!" Would this involve arrays or is there an easier method?
Last edited on
closed account (48T7M4Gy)
display the lowest score(guesses)


This is a little more complicated depending how you want to handle it.

Alternative 1.
Using the counter system add another new int HighestScore

So for each game if the value of counter at the end of a particular game is greater than HighestScore then HighestScore becomes counter. (Obviously counter need to be set at zero after that and at the start of a new game. You can print out the HighestScore at any time.

Alternative 2.
Is the same as 1 except you might want the HighestScore to be 'persistent' which means saving the latest HighestScore to a file. The value on the file can be re-loaded at the start when the program is re-run at a later time. You could also record player results that way too.

Thanks for tidying up the multiple posts. :)
Last edited on
How would I do alternative one I'm thinking
int highest score;
Very new to forum, not sure how to pick out specific code.
if(guess==num)
{
cout<<"You guessed the correct number."<<endl;
isGuessed=true;
if(numberofguesses<=highestscore)//newline, not sure if this is correct I'm thinking something along those lines. Also how would I set the counter to 0 each time?
{
highestscore++;//counter to add tries?
}
cout<<"It took you "<<numberofguesses<<" "<<" tries to guess the correct number!"<<endl;
cout<<"Would you like to play again? Enter Y/N"<<endl;
cin>>answer;
if(answer='N')
{
or would the counter go in the yes else if?' I have my counter outside the while, not sure if I need ot move it.
Last edited on
closed account (48T7M4Gy)
Sweet! I do have that already :D"numberofguesses++"

Good/great we're on the same page.

I want to keep track of the number of guesses each time say the first time the program is run the person guesses 12 times, the next time(saying yes to the question) the person guesses in 10 tries. Then a cout statement would say "your low score is 10, congratulations!" Would this involve arrays or is there an easier method?


No you don't need arrays. That's what the variable Highest Score is about. You would only need an array or similar if you had multiple HighestScores for example.

(BTW Highest means lowest number of guesses but I guess you realised my terminology error.)
I really only want it to do it once. Where should this go?
This failed completely but this is what I am thinking.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if(guess==num)
	{
		cout<<"You guessed the correct number."<<endl;
		isGuessed=true;
		if(numberofguesses<highestscore)
		{
		
		highestscore=numberofguesses;//if statement that says exactly that 
	}
		cout<<"It took you "<<numberofguesses<<" "<<" tries to guess the correct number, while your highestscore is"<<highestscore<<endl;
		cout<<"Would you like to play again? Enter Y/N"<<endl;
		cin>>answer;
		if(answer='N')
		{
		
			cout<<"Thanks for playing!"<<endl;
	
		}
	    else if(answer='Y')
		{
			playagain=false;
			numberofguesses=0;//Setting the guesses to 0?
		}

will clear up terminology later, just want this to work. :D
Last edited on
closed account (48T7M4Gy)
How would I do alternative one I'm thinking


You really should try it out.

However, declare highest score at the start of main with all the other variable declarations. ie int LowestScore = 50000; (Pick a VERY high number)

Now, at the end of a particular game just after you statement about correct answer, then test with the if statement. This all apears to be as you wrote above.

Where you are mistaken is you don't increment LowestScore, you change its value to the current number of guesses if the current number of guesses is less than the Lowest score.

So if the first game takes 9 guesses Lowest Score will be changed from 5000 to 9 and there you go!

It seems the number of guesses is being added on each time? Would that throw off the lowest score? I want the guesses to be reset to zero, if I do change that would the low score still be correct? Thanks again for your help. C++ is not my forte by far.
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
	int lowestscore=5000000;
 int numberofguesses=0;
 bool playagain=true;
 char answer;
int guesses;
	int num;
	int guess;
	bool isGuessed;
	srand(time(0));
	num=rand()%1000;
	isGuessed=false;
	while(!isGuessed, playagain=true){
 
	cout<<"Enter an integer greater than or equal to 0 and less than 1000"<<endl;
	cin>>guess;
	numberofguesses++;
	if(guess>1000 or guess<=-1)
	{
		cout<<"Please enter proper number, your number is out of range"<<endl;
		break;
	
	}

	if(guess==num)
	{
		cout<<"You guessed the correct number."<<endl;
		isGuessed=true;
		if(numberofguesses<lowestscore)
		{
		
		lowestscore=numberofguesses;
	}
		cout<<"It took you "<<numberofguesses<<" "<<" tries to guess the correct number, while your lowest is "<<lowestscore<<endl;
		cout<<"Would you like to play again? Enter Y/N"<<endl;
		cin>>answer;
		if(answer='N')
		{
		
			cout<<"Thanks for playing!"<<endl;
	
		}
	    else if(answer='Y')
		{
			playagain=false;
			
		}
		
	}

	 else if (guess<num)
	cout<< "Your guess is lower than the number. Try again."<<endl;
	
	else
	cout<< "Your guess is higher than the number. Guess again!"<<endl;

}
}
closed account (48T7M4Gy)
C++ is not my forte by far.
C++ is a bit of a pig at times but I think your problem might not be quite so language specific.

The problem seems to be you are not resetting numberofguesses = 0;

You have to put that line in before any new game starts ( and after the lowestscore is updated if you decide to do it later in the loop. )
Last edited on
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
}

	if(guess==num)
	{
		cout<<"You guessed the correct number."<<endl;
		isGuessed=true;
		if(numberofguesses<lowestscore)
		{
		
		lowestscore=numberofguesses;
	}
numberofguesses=0;//would that go here??? :)
		cout<<"It took you "<<numberofguesses<<" "<<" tries to guess the correct number, while your lowest is "<<lowestscore<<endl;
		cout<<"Would you like to play again? Enter Y/N"<<endl;
		cin>>answer;
		if(answer='N')
		{
		
			cout<<"Thanks for playing!"<<endl;
	
		}
	    else if(answer='Y')
		{
			playagain=false;
			
		}
		
	}


is that correct?(after testing it, it counts guesses as 0. Should this go in an if statement. If so where? Yeah, I am a complete noob, I only need one C++ credit for my cybersecurity major. IMO most companies use UIs with all of this mapped out. Of course having this would be helpful no doubt. Edit, just had to move that statement after where it displays it. I do have another question, how do check for incorrect input such as letters or characters, I did a brief error check such as the for statement seen above, but not sure about this. Will marked finished after this, I greatly thank your help.
Last edited on
closed account (48T7M4Gy)
is that correct?

The short answer is yes but the real answer is you should take control of your program and test it.

Take a break.
It's hard, I need to keep pushing. The program is "finished", but I would like to check for errors as above. I just want to get this credit and be done, this program took me around 5 hours sadly. To be honest this is the most stress I have had in a while. Programming should be fun, but I cannot find a way to enjoy it and half of the errors I do not understand and then that time is spent digging on Google for a list of error meanings.
Last edited on
closed account (48T7M4Gy)
5 hours isn't bad, look on the bright side some people take days.
True, is the error checking for letters and symbols a quick addition or no? Will marked solved if need be.
closed account (48T7M4Gy)
if(answer =='Y')
You've forgotten the = vs == in a few places.

Will marked solved if need be. 

I don't get a commission.
Are you familiar with textfiles and input with those?
Pages: 12