Hey all,

I just wanted to know if I should use a different process to run my loop. I use a while loop until the AI figures out the number.

I know there is a more efficient and easier way to make this loop happen with BOOLEAN.
Setting while(bool = 1) and having if/else/switches to change the bool = 0 to quit the loop.

When I do have a condition to set the loop = 0 (false), my loop continues to run though!
Can anyone provide me with a fix, or suggestion that I do not know about?

Thanks!

#include <cstdlib>
#include <iostream>

using namespace std;

int Toohigh(int x, int y);
int Toolow(int x, int y);

int main(int argc, char *argv[])
{
    int number = 19;  //INTERCHANGABLE THE MAGIC NUMBER (1-100)
    int MAX = 100;
    int MIN = 1;
    int guess = 0;
   
    int counter = 1;
   
   do
   {
    
   
    if (number < guess){
               MAX = guess; // Changes MAX range
               guess = Toolow(MAX, MIN);
               cout << "The computer is too high\n";
               }    //Too Low
    else if (number > guess){
               MIN = guess; // Changes MIN range
               guess = Toohigh(MAX, MIN);
               cout << "The computer is too low\n";
                 }     //Too high    
        
     cout << "Attempt number: " << counter << 
   "\tComputer guesses the number " << guess << endl;
    
    
    counter++;   
    }while(number != guess);
    
    if (number == guess){
               cout << "The number is " << guess << " and the computer gussed it"
               " on attempt #: "<< counter-1 << "!\n";
               }
    cin.get();   
}

int Toohigh(int x, int y){ //(MAX, MIN)
    int z;
    z = x - y;
    z = x -(z/2);
    
    return z;
    }
    
int Toolow(int x, int y){
    int z;
    z = x - y;
    z = (z/2) + y;
    
    return z;
    }

Recommended Answers

All 2 Replies

>>When I do have a condition to set the loop = 0 (false), my loop continues to run though!

There are two keywords you need to know about: break : this instruction will terminate the loop. In other words, the execution "jumps" from where the 'break' statement appears to the line immediately following the end of the loop. continue : this instruction will skip the remainder of the current iteration and move on to the start of the next iteration of the loop (i.e. goes back to the start of the loop, and does also check the condition of the loop and evaluates the for-loop expression (third slot) too, if such is the case).


Besides that question you asked, I have no idea what the relation between the code you posted and the question you asked is.

So you can use

break;

outside of a switch?

I didn't post the boolean loop because it was jacked...I posted what worked :-\

>>When I do have a condition to set the loop = 0 (false), my loop continues to run though!

There are two keywords you need to know about: break : this instruction will terminate the loop. In other words, the execution "jumps" from where the 'break' statement appears to the line immediately following the end of the loop. continue : this instruction will skip the remainder of the current iteration and move on to the start of the next iteration of the loop (i.e. goes back to the start of the loop, and does also check the condition of the loop and evaluates the for-loop expression (third slot) too, if such is the case).


Besides that question you asked, I have no idea what the relation between the code you posted and the question you asked is.

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.