Using loops/going back to beginning

Hey everyone. I'm quite new to programming and need some help. I'm not going to my exact code to prevent getting in trouble.

I'm trying to make a game-type program. The computer creates a random number between 1 and 100 (randNum). The player enters their guess (guessNum). If/when the user enters the computer's number, then the player loses (yeah, backwards logic.) The computer asks the player for another guess and adjusts the limits.
If the computer states a limit that is the actual randNum, then the player wins. (Below, this would mean if the computer said "Enter a number between 38 and 40:" Since it had to use the randNum, the player wins.)

Example (if the randNum was 38):
Enter a number between 1 and 100.
16
Enter a number between 17 and 100:
75
Enter a number between 17 and 74:
.....and so on and so on.....
As you can see, the limit changes, +1 or -1, depending on the guess.

Problem: The game needs to keep going until the player wins or the computer wins, meaning it's not count-controlled. I can't figure out which type of loop/s to use. I know I need at least 1 loop, but probably 2 or more.
-Loop back to beginning if the player wants to play again
-Loop used to keep asking the player to enter a number, check validity, etc...
*The code below probably isn't in the right order. I know it will change order depending on which kind of loop/s it needs.


Here is the stuff I know I need. I used "if" statements because that's my logic behind it. I don't know how to tie it all together using a loop or multiple loops:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
if (guess == randNum) //player guesses the randNum. Player loses.
 {
   cout << "That's my number! Play again? [Y/N]" << endl;
   cin >> playAgain;
 }
if (lowLimit > guessNum || guessNum > highLimit) //if player enters a number outside of the limits
 {
   cout << "You must enter a number between 1 and 100!" << endl;
   cout << "Press any key to continue..." << endl;
   return 1; //Kicks them from the program
 }
if (guess < randNum) 
 {
   cout << "Enter a number between " << guess + 1 << " and " << highLimit << endl;
   cin >> guess;
   lowNum = guess + 1;
 }
if (guess > randNum)
 {
   cout << "Enter a number between " << lowLimit << " and " << guess - 1 << endl;
   highNum = guess - 1;
 }
if (guess == highLimit || guess == low) //player's guess is one of the limits. The computer wins
 {
   cout << randNum << " is my number. You win!" << endl;
 }


Thank you in advance! I apologize it's so long and if I need to specify anything, then just ask.
I'm a newb but couldn't you just use a while statement? Without seeing the whole code...for example....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
while (guess != randNum || ++randNum || --randNum){

cout <<  "Please enter another number" << endl;
cin >> guess;

if (guess < randNum) 
 {
   cout << "Enter a number between " << guess + 1 << " and " << highLimit << endl;
   cin >> guess;
   lowNum = guess + 1;
 }
if (guess > randNum)
 {
   cout << "Enter a number between " << lowLimit << " and " << guess - 1 << endl;
   highNum = guess - 1;
 }
if (lowLimit > guessNum || guessNum > highLimit) //if player enters a number outside of the limits
 {
   cout << "You must enter a number between 1 and 100!" << endl;
   cout << "Press any key to continue..." << endl;
   return 1; //Kicks them from the program
 }

}
if (guess == highLimit || guess == low) //player's guess is one of the limits. The computer wins
 {
   cout << randNum << " is my number. You win!" << endl;
 }



This will loop back to the top of the while statement after it's finished as long as the parameters are true. Once false, assuming they just choose of the the triggering numbers, it will leave the while statement and continue into your winning function.

I know I have mistakes but this is a quick rough draft...sorry if im way off.
Last edited on
Thanks! That's working better than what I had before.
Still a few problems:
1. It keeps telling me I need to initialize 'guess'. Not sure what I need to do there.
2. It's accepting negative numbers. It need to only accept number between lowLimit and highLimit.
3. Running it, it stops after I enter two different guesses. Below is the exact words it says. Not sure why it's quitting after two times...

Enter a number between 1 and 100 then press enter to continue.
5
Enter a number between 6 and 100
9
You must enter a number between 10 and 100!
Press any key to continue...
Press any key to continue . . .


Figuring out these few questions would help a lot. Thank you for the help already!!!!!!!!!!

*EDIT:
Andddddd......when I guess the randNum itself, meaning I guess the computer's number, it keeps going. Instead, it should say the computer loses. That part isn't in the new code. I'm not sure where this needs to be added in the loop, though.
Last edited on
Anyone else? Thanks.
You did not show the entire program so I cannot determine the answers to some of your questions. Initializing guess is important since you are testing it at the beginning of the loop. What is the default value?

It is simple to build an input loop that tests for values within a range as well as invalid values. Take a look at this. You can copy and paste some of the code from this example and taylor it. I recommend creating a loop for the input into guess within the beginning of the while loop. Do this only at that point. The loop will return to the top when the guess isn't correct so there is no point in duplicating that code in multiple places.
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.3
Topic archived. No new replies allowed.