Hi all
I am a total newbie and am self teaching myself. I worte a program that lets the user guess a random number and then asks if if they want to play again.\
Here is the code:

#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <ctime>
#include <conio.h>

using namespace std;

int main()
{
char name[15], answer[4], playAgain[4];
srand(time(0));
int number=rand()%100;
int guess=-1;
int trycount=0;



 cout<<"please enter your first name: ";
 cin.getline(name, 15);
 cout<<"hello " << name <<"\n";
 cout<<"did you have a good day? yes/no " ;
 cin.getline ( answer, 4 );

  if
   ( strcmp ( answer, "yes" ) == 0)
   cout<< "I am so glad you had a good day" <<endl;
  else
   cout<< "I am sorry you had a bad day" <<endl;

 cout<<"Would you like to play a game? yes/no ";
 cin.getline (answer, 4 );

  if (strcmp (answer, "yes") ==0)
  {
     cout<< "Great, this game is find the number " <<endl;
     cout<< "The number will be between 0 and 100" <<endl;
     cout<< "You have 8 tries " <<endl;
      do
      {
       while(guess!=number && trycount<8)
       {

         cout<<"Please enter a guess: ";
         cin>>guess;
         cin.ignore();

         if(guess<number) cout<<"Too low"<<endl;
         if(guess>number) cout<<"Too high"<<endl;

         trycount++; //adds 1 to each try
       }
      if(guess==number)
       cout<<"You guessed the number" <<endl;
      else //{
            cout<<"Sorry, the number was: "<<number <<endl;
           //}
         cout<<"would you like to play again? yes/no ";
         cin.getline (playAgain, 4);
         

       }
      while (strcmp ( playAgain, "yes" ) == 0);
         //cin.getline (playAgain, 4);

     cout<<"Thanks for playing ";

     return 0;
   }
   else {
         cout<< "OK see you next time" ;
        }



return 0;
}

code compiles and runs -- the first time. The problem is when it gets to the playAgain section it only loops to the you guessed the number section.
After searching the internet and forums I realize my guess = number and trycount are not being reset. so I get an output like this:

You guessed the number
would you like to play again? yes/no yes
You guessed the number
would you like to play again? yes/no yes
You guessed the number
would you like to play again? yes/no no
Thanks for playing

I have no idea how to implement that.
Do I put

for (int (guess== -1 && trycount== 0))

before

}
      while (strcmp ( playAgain, "yes" ) == 0);

I am just not sure of how to go about resetting guess and trycount or where it needs to go in the code.
Thanks for any help and insight

Hello pdenman,
Reset trycount and guess in the starting of the do-while loop.

do
{trycount=0;
 guess=-1;
 while(guess!=number && trycount<8)
 //the code you posted
}

Hello pdenman,
Reset trycount and guess in the starting of the do-while loop.

do
{trycount=0;
 guess=0;
 while(guess!=number && trycount<8)
 //the code you posted
}

That did the job. I also had to reset

number=rand()%100

or else it kept the same random number all the time
Thank you so much Arbus for the quick reply and the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.