### How can you switch between Boolean true/false logic within a do/while loop?

Hello, I have a hit a snag in a number guessing game program. I was given a half-completed program and told to use functions to complete the missing pieces. It looks unwieldy, but this is how it is supposed to be. I have temporarily made the random guess value visible for troubleshooting purposes.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465`` ``````#include #include #include using namespace std; int welcome() { cout << " Welcome to the hi-low game!" << endl; cout << " I have picked a number between 1 and 100." << endl; cout << " Let's see how many guesses it takes you to get it." << endl; cout << " Each time, I'll tell you if you're right, too low, or too high." << endl; // set correct to be a random number between 1 and 100. int correct = rand() % 100 + 1; cout << correct << endl; // send back the answer so it can be used in main. return correct; } int getGuess() { int guess; // ask the user for their guess and return it. // you'll need: cout, cin, and return cout << "Enter a guess between 1 and 100 : "; return guess; } bool checkGuess(int guess, int correct){ cin >> guess; // check whether guess is correct. tell them if it's too high, // too low, or correct. then return true if guess is correct and false // if it's wrong. // you'll need: if, else if, else, cout, return if (guess > correct) { cout << "Too high!" << endl; return false; } else if (guess < correct){ cout << "Too low!" << endl; return false; } else { return true; } } int main() { srand(time(NULL)); int correct; correct = welcome(); int guess; bool done=false; do { // call the getGuess function to get their guess. getGuess(); // call checkGuess to check if their guess is correct, and set done to be what checkGuess returns checkGuess(guess, correct); done = false; //either true or false } while (!done); cout << "Congratulations, you got it!" << endl; return 0; }``````

The issue lies within this piece of code:
 ``12345`` `````` checkGuess(guess, correct); done = false; //either true or false } while (!done); cout << "Congratulations, you got it!" << endl; return 0; }``````

I need to manipulate the Boolean variable done so that it registers as false when the user inputs a number higher or lower than the randomly selected value. However, if the user guesses correctly, done will become true and the program will end. I am not sure what will work after trying several methods to fix this.

As it stands now, the program will not terminate, and if I set done equal to true like so:
 ``12345`` `````` checkGuess(guess, correct); done = true; //either true or false } while (!done); cout << "Congratulations, you got it!" << endl; return 0; }``````

Every number the user inputs will register as correct instead of the one right guess. I know there is a simple solution, but I have not figured it out yet.
What exactly do you want it to do, and what is it doing?

When you call the checkGuess function it looks like after it implements the function you are saying done = false; Therefore will it ever get true? I'm confused.
Last edited on
I need a way to program conditions that either tell the user to guess a number again if their input is too high or too low, or if the guess is right, stop prompting them to try again and end the program.

The variable " done " is initially set to false, as in the program should not be done until a correct guess is given. Its value should depend on what checkGuess returns: either true or false. Wrong answers will make checkGuess return as false, continuing the loop. Answering with correct guess will cause the checkGuess function to return as true.

Once done becomes true, it activates another line of command that only initiates if done is no longer set to "false". This ends the program.

`} while (!done);`
cout << "Congratulations, you got it!" << endl;
return 0;
}

It's a bit of backwards logic that confused me, too. However, I need to discover a way to make this program detect the difference between continuing the loop and stopping at a right answer, which, again relies on the value of done.
Last edited on
You could just change the do-while loop to the following, to get rid of the need for a 'done' boolean variable at all:
 ``123`` ``````do { getGuess(); } while (!checkGuess(guess, correct));``````
Thank you in the sincerest way possible. You've cleared up a wall-banger of a program. It's running as it should.
Topic archived. No new replies allowed.