beginning in c++, i have a question of a recent program

for this program i am trying to create a game where the computer try's to guess a number 1-100 that the user has chosen and i have it running good.

but where i have a problem is at the end, i want to give the user the option to play again or quit
so i have put another loop where this is done, but the problem is when user input yes i do not know how to send program back to beginning.
so it will guess again for users number is this possible, how can i fix please help me.

here is what i have

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


int main()
{
    srand(static_cast < unsigned int >(time(0)));
	int lowerBound = 1;
	int upperBound = 101;
	bool done = false;
	char ans;
	bool found = true;

do 
 {
    int range;
    int guess;
	range = upperBound - lowerBound;
	guess = upperBound - (rand() % range);
	cout << "Is your number?    " << guess << "  (l/h/y)   " << endl;
	cin >> ans;
	 
	
	if (ans == 'l')
		{
	        upperBound = guess - 1;
			done = false;
			cin.get();
	    }
	
	
	 if (ans == 'h')
	    {
		   lowerBound = guess + 1;
		   done = false;
		   cin.get();
	    }
	
	 
	 if (ans == 'y')
	   {
		cout << "yes!!!! i got it.   " << endl;
		done = true;
	   }
}while (!done);

 do 		
	 {
		cout << "do you want to play again? (y/n)" << endl;
		cin >> ans;

			if (ans == 'n')
			 {
				done = true;
			 }
	
	 
			if ( ans == 'y')// this is my problem once inputs yes i want to go back to beginning of program
			 {
				done = false;
			 }
	     	
	
	 }while (!done);

 
cin.get();
return 0;
}

Put one do()while() within the other do()while() so it keeps looping unless you are done your game loop and when you do not want to play again.

Doing this you will want to have two variables instead of having both as done.

alright i edited, still having trouble once select yes option it does not go to top of first loop just keeps looping the second.

code repost

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


int main()
{
    srand(static_cast < unsigned int >(time(0)));
	int lowerBound = 1;
	int upperBound = 101;
	bool done = false;
	char ans;
	bool found = false;



do 
 {
    int range;
    int guess;
	range = upperBound - lowerBound;
	guess = upperBound - (rand() % range);
	 
	 cout << "Is your number?    " << guess << "  (l/h/y)   " << endl;
	 cin >> ans;
	 
	 // is devidving by zero because the range is zero so must use else statement if range equals then we know the ans 
	 // then output the ans------where do i put it then
	 

     
	 if (ans == 'l')
		{
	        upperBound = guess - 1;
			done = false;
			cin.get();
	    }
	
	
	 if (ans == 'h')
	    {
		   lowerBound = guess + 1;
		   done = false;
		   cin.get();
	    }
	
	 
	 if (ans == 'y')
	   {
		cout << "yes!!!! i got it.   " << endl;
		done = true;
	
		do
		{
       
			cout << "do you want to play again? (y/n)" << endl;
			cin >> ans;

				if (ans == 'n')
				 {
				   cout << "Thank you for playing...... press <enter> key to exit"<< endl;
                   cin.get();
                   found = true;
			      
				 }
	
				if ( ans == 'y') 
				 {
				   found = false;
				 }
			
         }while (!found);
	    }
	 
	}while (!done);
 
cin.get();
return 0;
}

Your positioning is the reason.

do
		{
       
			cout << "do you want to play again? (y/n)" << endl;
			cin >> ans;

				if (ans == 'n')
				 {
				   cout << "Thank you for playing...... press <enter> key to exit"<< endl;
                   cin.get();
                   found = true;
			      
				 }
	
				if ( ans == 'y') 
				 {
				   found = false;
				 }
			
         }while (!found);

Because this loop is inside the other, you're not looping the other one. Your "quit game?" check needs to be the first loop, and the game itself needs to be the second loop.

do
{
 do 
 {
  // program
 }while(check for right answer);
}while(check if the player wants to exit);

This is because a loop executes everything inside of the brackets {}, then repeats it if the statement at the bottom is true. Since you want the game to loop it's checks, then loop the game itself if the player wants to, you need to execute the game loop then the guess loop.

Edited 5 Years Ago by Red Goose: n/a

RedGoose thanks for ans,
you mean like this right?

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


int main()
{
    srand(static_cast < unsigned int >(time(0)));
	int lowerBound = 1;
	int upperBound = 101;
	bool done = false;
	char ans;
	bool found = false;
    


do 
 {
   cout << "do you want to play again? (y/n)" << endl;
   cin >> ans;
     
	 if (ans == 'n')
	  {
        cout << "Thank you for playing...... press <enter> key to exit"<< endl;
        cin.get();
        found = true;
			      
	  }
	
	 if ( ans == 'y') 
	  {
		 found = false;
	  } 
  }while (!found);    
          
          do 
           {
             int range;
             int guess;
	         range = upperBound - lowerBound;
	         guess = upperBound - (rand() % range);
    
             cout << "Is your number?    " << guess << "  (l/h/y)   " << endl;
	         cin >> ans;
	 
	 // is devidving by zero because the range is zero so must use else statement if range equals then we know the ans 
	 // then output the ans------where do i put it then

     
	            if (ans == 'l')
		         {
	               upperBound = guess - 1;
		        	done = false;
			        cin.get();
	             }
	
	
	          if (ans == 'h')
	            {
		          lowerBound = guess + 1;
		         done = false;
		         cin.get();
                }
	
	 
	        if (ans == 'y')
	            {
		          cout << "yes!!!! i got it.   " << endl;
		          done = false;
                }
	    	
         }while (!done);	
		
	
         

			
cin.get();
return 0;
}

another trouble i am having now is once guess number and asks the first time if want to play again but if click no program will keep going
but is weird b/c if click yes the first time then no a second time it works fine

Edited 5 Years Ago by lgonzo: n/a

No. Your loops should be inside of each other. What's happening there is you're going to loop the "do you want to play again?" message until they hit N, then the actual game will start. You need to play the second loop within the first, asking if they want to play again before the end of the first loop.

Edited 5 Years Ago by Red Goose: n/a

thanks alot red goose, helped alot able to figure out

This question has already been answered. Start a new discussion instead.