am newb in c++ trying to write simple c++ program that a computer, try to guess a number i have chosen by it producing rand number and me telling me whether it is to low, high, or right and using do while loops we are suppose to produce it

now my trouble is, in working with the loop that tells the computer its guess is to low,
once i have told it is lower then that guess, it should pick a lower number

but i cannot figure out how to do that check out my code (attachment) if i tell it is to low, then i want next guess to be lower but it is not the case

i figure that my problem is that i cannot set the new guess to be in the new range, which should be from the lower limit to minus 1 of the last guess

Recommended Answers

All 4 Replies

Please post your code directly using code tags. Preferably, simplify the code as much as possible so that it only demonstrates the relevant parts.

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

using namespace std;
bool done = false;
char ans;
bool found = true;


int main()
{
    srand(static_cast < unsigned int >(time(0)));
	
	int lowerBound = 1;
	int upperBound = 101;

	int range;
	int guess;
   
do 
 {
	 
	 range = upperBound - lowerBound;
	 guess = upperBound - (rand() % range);
	  
	 cout << "Is your number:    " << guess << "  (l/h/y)   ";
	 cin >> ans;

	 if (ans == 'l')
		{
			done = false;
		}
			
}while (!done);


 cin.get();
 return 0;
}

think simpliest could have

1) There is no input that tells the program if the guess is low nor high.
2) there is no attempt to change the bounds if the guess is high or low.

You need to at least attempt to solve the problem to get help. As it is we cannot help you correct what's not there.

i have been trying, just starting with c++ so am very inexperienced

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

using namespace std;
bool done = false;
char ans;
bool found = true;


int main()
{
    srand(static_cast < unsigned int >(time(0)));
	
	int lowerBound = 1;
	int upperBound = 101;
	int range;
	int guess;
	int guessLower;

   
do 
 {
	 
	 range = upperBound - lowerBound;
	 guess = upperBound - (rand() % range);
	 cout << "Is your number:    " << guess << "  (l/h/y)   " << endl;
	 cin >> ans;

	
	 //if anewers is l
	 if (ans == 'l')
		{
			guessLower = lowerBound + (rand() % range);
			cout << "Is your number:   " << guessLower << "   (l/h/y)  " <<  endl;
			cin >> ans;
			done = false;
			cin.get();
		}
	// started trying  to select option for h ansewer
	 if (ans == 'h')
	    {
		   cout << "ummm ok let me try a higher number again    " << endl;
		   done = true;
		   cin.get();
	    }

	 //started trying if ans inputed y
	 if (ans == 'y')
	   {
		cout << "yes!!!! i got it.   " << endl;
		done = true;
		cout << "do you want to play again? (y/n)" << endl;
		cin >> ans;

			if (ans == 'n')
			 {
				done = true;
			 }
	
	 
			if ( ans == 'y')
			 {
				done = false;
			 }
			
		}

 }while (!done);


 
cin.get();
return 0;
}

one more question
from now how would i be able to set new boundaries for newer guess

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.