• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Play again function help?

 
Dj Lee
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've made a hi-lo guessing game and I'm having trouble with the play again function using the loop.

Here's my code:


line 59 to 73 is where I inserted the play again function.

If the user correctly guesses the number right, the program prints "Play again? Y or N?" twice...
and I'm allowed to enter an input but it just loops through "play again? y or n?" instead of actually starting the program again.

What am I doing wrong?
 
Greg Brannon
Bartender
Posts: 563
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was surprised to see the second 'do' loop. With each prompt in the main do loop, you've already instructed the user to enter -1 if he/she wants to quit. That by itself could terminate the game.

Then, your second do loop - just as you said - continues to loop, asking the player if they want to continue to play. That's all the loop does. There's no way for a true answer to get the logic back to the main do loop.

So, figure out what you meant to do with the -1 selection in the main do loop. Or, if you wan to ask the user if he/she wants to continue, that would be a line at the end of the main do loop that sets the do loop flag to continue or exit, similar to what you've done in the second do loop. You don't need both exit paths, just one of them in the main do loop.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic