AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

If Else Confusion

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
rgblair11
Guest





PostPosted: Fri Aug 13, 2004 9:53 am    Post subject: If Else Confusion Reply with quote

I'm trying to learn about program flow by creating a simple "guess the number game" (i.e. define a random number, have user guess, display higer or lower).

Here is the code I'm starting with. Problem is, no mater what the guess is, MsgBox Higher displays and then the "guess" dialog starts again. I realize I'm doing something complety wrong, but I can't figure it out. Here is the code:

InputBox, min, Enter Minimum Value, Enter Minimum Value,
Inputbox, max, Enter Maximum Value, Enter Maximum Value
random, num, %min%, %max%
MsgBox, The Number is %num% ; used for testing, I want to know what the number is
GUESS:
InputBox, guess, Guess, Enter Guess Between %min% and %max%
If %guess% < %num%
{
MsgBox, Higher
Gosub, GUESS
}
else if %guess% > %num%
{
MsgBox, Lower
Gosub, Guess
}
Back to top
jordis



Joined: 30 Jul 2004
Posts: 78

PostPosted: Fri Aug 13, 2004 10:10 am    Post subject: Reply with quote

rgblair11,

the problem is in these lines:
If %guess% < %num%
else if %guess% > %num%

you should get rid of the %s in guess, like:
If guess < %num%
else if guess > %num%

jordi
Back to top
View user's profile Send private message
BoBo
Guest





PostPosted: Fri Aug 13, 2004 10:52 am    Post subject: Reply with quote

IfEqual, var, value
same
if var = value

taken from the help.

As I know you use the percentage char in cases where literals could be used as well e.g. with the Send command

Code:
Send, Myname // This will send the string "Myname"

MyName = rgblair11
Send, %Myname% // That will send the variables content "rgblair11"
Back to top
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10467

PostPosted: Fri Aug 13, 2004 11:04 am    Post subject: Reply with quote

Also, you should not do "Gosub Guess" in this case. You should either use "Goto Guess" or enclose the contents in a loop as shown below:
Code:
InputBox, min, Enter Minimum Value, Enter Minimum Value,
Inputbox, max, Enter Maximum Value, Enter Maximum Value
random, num, %min%, %max%
MsgBox, The Number is %num% ; used for testing, I want to know what the number is
Loop
{
     InputBox, guess, Guess, Enter Guess Between %min% and %max%
     If guess < %num%
          MsgBox, Higher
     else if guess > %num%
          MsgBox, Lower
     else  ; Answer is correct, so break out of the loop.
          break
}
MsgBox You guessed it!
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group