am new to c++, and am having trouble with if else statement
for this code i am writing a program that makes the computer guess a number you the user has choosen
my program works fine, but when i get in trouble is when my range gets down to zero
it will pick no new number and breakdown

so i know i need a else statement that says if the range is zero if should stop loop and it should know the number is now upperbound
and that is where i am stuck, i dnt know where i should put the statement
or exatly how it should be done
this is what i was thinking

else
range = 0
guess = upperBound


pplse help

#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;
	 
	 // 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 anewers is l
	 if (ans == 'l')
		{
	        upperBound = guess - 1;
			done = false;
			cin.get();
	    }
	
	// started trying  to select option for h ansewer
	 if (ans == 'h')
	    {
		   lowerBound = guess + 1;
		   done = false;
		   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;
}

Recommended Answers

All 7 Replies

Try this .........

#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;
    int count = 0;
	//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;

	 // 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 anewers is l
	 if(range ==0){
		 done = true;
	 }
	 if (ans == 'l')
		{
	        upperBound = guess - 1;
			done = false;
			cin.get();
	    }

	// started trying  to select option for h ansewer
	 if (ans == 'h')
	    {
		   lowerBound = guess + 1;
		   done = false;
		   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;
			 }

		}

  count++;

 }while (!done);


cout << "Number of execution is " << count++ << " Times \n";
cout <<"Programme terminated \n";
cin.get();
return 0;
}

still had problems trying,
is it not possible to do with else statement?

Are you executing the code by hand to see what happens with the number that doesn't work? Follow each statement with pencil and paper and write down each variable and watch it change. You should find out where it goes wrong.

i know the number that doesnt work is when range gets changed to zero

so i want program to know that when range equals zero it doesnt have to guess to more, it should know that the number
i know you can do this with the else statement and that is where my trouble is how input that statement in my code.

You gave yourself the answer there...
IF the range is 0, then it doesn't have to guess any more.

Looking at your original code, at line 23, you compute the range.
After computing, test the range. If it is not 0, do what you have coded (this is in the if) put the else after doing what you need to do and continue.

...your code to line 23
if (range != 0) {
... your code that states guess and asks for l/h/y
} else {
... new code to output the answer
  ans = 'y'; // set to yes so the next group of ifs will "do the right thing"
}
... your code that checks what ans is

i know the number that doesnt work is when range gets changed to zero

so i want program to know that when range equals zero it doesnt have to guess to more, it should know that the number
i know you can do this with the else statement and that is where my trouble is how input that statement in my code.

Then I will rephrase:
With pencil and paper, go through your code line by line using the number that doesn't work and see what happens when you get to that range. What needs to change in the code right there.

You gave yourself the answer there...
IF the range is 0, then it doesn't have to guess any more.

Looking at your original code, at line 23, you compute the range.
After computing, test the range. If it is not 0, do what you have coded (this is in the if) put the else after doing what you need to do and continue.

...your code to line 23
if (range != 0) {
... your code that states guess and asks for l/h/y
} else {
... new code to output the answer
  ans = 'y'; // set to yes so the next group of ifs will "do the right thing"
}
... your code that checks what ans is

exatly what trying to do thanks helped alot

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.