Skip to main content
GameDev.net gamedev.net
🔒 Locked

endless loop...

Started by random-decay Oct 14, 2001 at 1:45 AM 3 replies 250+ views
Original Post
random-decay
random-decay
I ask my user for a bet, it must be greater than or equal to 2, or less than or equal than their pot. Let's say they enter a character. BAM! Endless loop! How do I fix this? Or they enter some number like 999999999999999999999999999999 Edited by - random-decay on October 14, 2001 2:46:20 AM Edited by - random-decay on October 14, 2001 2:48:51 AM
krez
krez
um, you gotta be more specific... what programming language? how are you getting this input? etc...
without knowing, all i can say is check the input before proceeding, and if it is not correct ask again.

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
random-decay
random-decay
using C++

  
// Ask player to bet - must be >= 2 or <= pot

do
{
cout << "Enter bet: ";
cin >> bet;

}
while ( bet < 2 || bet > pot );
Oluseyi
Oluseyi
How is bet declared? How is it being read? If bet is a char then it may always fall outside your range and thus loop indefinitely. I suggest you run your program through the debugger (if you don't know how to use the debugger, this is a good time to learn!)

You also have the interesting logic bug of doing a logical OR rather than an AND in your condition statement.

Edit: if your user enters a character, it may convert to its ASCII equivalent in integer context (ah, Perl...)


Edited by - Oluseyi on October 14, 2001 4:04:24 AM
Fallen_Angel
Fallen_Angel
Try using this. There''s probably a better way of doing it but it should work for now.

  
int bet = 0;
BOOL betOK = FALSE;
while (!betOK)
{
cout << "Enter bet: ";
cin >> bet;
if ((bet >=2) && (bet <= pot))
betOK = TRUE;
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.