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;
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.
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?
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.
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.
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.)
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;
}
elseif(answer='Y')
{
playagain=false;
numberofguesses=0;//Setting the guesses to 0?
}
will clear up terminology later, just want this to work. :D
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.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace 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;
}
elseif(answer='Y')
{
playagain=false;
}
}
elseif (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;
}
}
}
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;
}
elseif(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.
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.