# Problem With IsNumber()

## Recommended Posts

Hi all, some time ago I spoke with some kind people of this forum because I wanted to start learning to program in Autoit. They handled me some guides and now I'm here stuck asking for help

My problem is about the IsNumber function. The problem may be related to the other IsXXXX functions but I've not tested it, or, maybe, it's just me using it wrong.

I am doing an exercise program. This program has a GUI with 3 buttons.

Clicking the first opens the Notepad and writes the current date and time.

The second asks you for 4 options first which are the 4 basic arithmetical operations. Then asks you for 2 operators and opens the windows calculator and types in the previous input given and closes the calculator after 6 seconds.

The third opens the notepad and types in the current date, time and the OS used.

The problem was given by a operator IsNumber not working, and I had to change the source because it wan't working also when it was fine. How I used the IsNumber: As i explained the problem is around the second button, as the other are pretty easy. When Button 2 is pressed it shows up a inputbox where you have to put 1 for Multiplication, 2 for division, 3 for Sum, 4 for subtraction. The control was an IF, checking if the input Is a number (Isnumber) and that its value its between 1 and 4, as there are opnly 4 options. There are other 2 Isnumber, just checking that the input for the other inputboxes was actually a number.

IE: 1st inputbox you type in 1, 2nd box you input 20, 3rd box you input 5. Calculator opens and types 20*5= and give result 100. After 6 seconds it closes.

have a look and test it yourself:

#include <GuiConstants.au3>
GUICreate("Multiselezione by NATTA",300,230)
GUISetState(@SW_SHOW)
\$bottone1=GUICtrlCreateButton("Opzione 1",10,10)
GUICtrlCreateLabel("Aprire il notepad e scrivere l'ora e la data, poi chiuderlo"&@CRLF&"senza salvarlo",15,40)
\$bottone2=GUICtrlCreateButton("Opzione 2",10,80)
GUICtrlCreateLabel("Aprire la calcolatrice e eseguire un calcolo a scelta tra"&@CRLF&"2 operatori con le 4 operazioni di base",15,110)
\$bottone3=GUICtrlCreateButton("Opzione 3",10,150)
GUICtrlCreateLabel("Aprire il notepad, scrivere l'ora e la data, il sistema"&@CRLF&"operativo e chiudere senza salvare",15,180)

While 1
\$Scelta = GUIGetMsg()

If \$Scelta = \$GUI_EVENT_CLOSE Then ExitLoop

Select
Case \$Scelta=\$bottone1

BlockInput(1)
WinWaitActive("Senza nome - Blocco note")
Send("Ora e data "&@CRLF&"{F5}")
Sleep(1500)
WinClose("Senza nome - Blocco note")
WinWaitActive("Blocco note")
Send("!n")
BlockInput(0)

Case \$Scelta=\$bottone2
If Not IsNumber (\$operatore2) and \$operatore2<1 and \$operatore2>4 Then
Msgbox(0, "Operatore errato", "Hai inserito un operatore errato!")
\$operatore1=InputBox("Inserire il primo operatore numerico","Primo Operatore:")
Elseif Not IsNumber (\$operatore1) Then
Msgbox(0, "Operatore errato", "Hai inserito un operatore errato!")
\$operatore3=InputBox("Inserire il secondo operatore numerico","Secondo Operatore:")
Elseif Not IsNumber (\$operatore3) Then
Msgbox(0, "Operatore errato", "Hai inserito un operatore errato!")
Else
BlockInput(1)
Run("Calc.exe")
WinWaitActive("Calcolatrice")
Sleep(400)
Switch \$operatore2
Case \$operatore2=1
Send(\$operatore1&"*"&\$operatore3&"=")
Case \$operatore2=2
Send(\$operatore1&"/"&\$operatore3&"=")
Case \$operatore2=3
Send(\$operatore1&"+"&\$operatore3&"=")
Case \$operatore2=4
Send(\$operatore1&"-"&\$operatore3&"=")
EndSwitch
BlockInput(0)
Sleep(6000)
WinClose("Calcolatrice")
Endif
Case \$Scelta=\$bottone3
BlockInput(1)
WinWaitActive("Senza nome - Blocco note")
Send("Ora e data"&@CRLF&"{F5}"&@CRLF&"Sistema operativo"&@CRLF&@OSVersion)
Sleep(1500)
Winclose("Senza nome - Blocco note")
WinWaitActive("Blocco note")
Send("!n")
BlockInput(0)
EndSelect
WEnd

As this is the whole source, after I will quote only the parts which I changed.

This solution gave me various problems, including the Isnumber not working. Try to put MsgBox(0,"text", IsNumber(\$Operatore2)) after this line

and see yourself that whatever input you put is, the result shown should be 0.

Then, later I had to use this solution to make the thing work:

Case \$Scelta=\$bottone2
while Not IsNumber(\$operatore2) or \$operatore2<1 or \$operatore2>4
Msgbox(0, "", "Operatore errato")
WEnd
\$operatore1=InputBox("Inserire il primo operatore numerico","Primo Operatore:")
while Int(\$operatore1)=0
Msgbox(0, "", "Operatore errato")
\$operatore1=InputBox("Inserire il primo operatore numerico","Primo Operatore:")
WEnd
\$operatore3=InputBox("Inserire il secondo operatore numerico","Secondo Operatore:")
while Int(\$operatore3)=0
Msgbox(0, "", "Operatore errato")
\$operatore3=InputBox("Inserire il secondo operatore numerico","Secondo Operatore:")
WEnd

And in a first moment it worked then it stopped, and this is another problem i never understood in Autoit. For example, the other day i copied by hand the source of a program. and checked word by word if it was the same. But mine didn't worked.

By the way when I Tried this, and didn't worked at all:

Case \$Scelta=\$bottone2
while Not IsNumber(\$operatore2) or \$operatore2<1 or \$operatore2>4
Msgbox(0, "", "Operatore errato")
WEnd
\$operatore1=InputBox("Inserire il primo operatore numerico","Primo Operatore:")
while Not IsNumber(\$operatore1)
Msgbox(0, "", "Operatore errato")
\$operatore1=InputBox("Inserire il primo operatore numerico","Primo Operatore:")
WEnd
\$operatore3=InputBox("Inserire il secondo operatore numerico","Secondo Operatore:")
while Not IsNumber(\$operatore3)
Msgbox(0, "", "Operatore errato")
\$operatore3=InputBox("Inserire il secondo operatore numerico","Secondo Operatore:")
WEnd

And putting this doesn't work at all

Another question i have is that there is the possibility of restarting a loop like this withouth having to do multiple whiles like i had to do:

I wanted to do like this

If variable1 it's not a number then

Elseif

If variable 2 it's

ETC ETC...

While Variable 1 it's not a number

While End (Wend)

While....

which is a little longer. There isnt a way to say autoit to restart the loop if the condition isn't verified.

Other question is:

When i click cancel in one of those Inputboxes, it keeps coming back. There is a way to make it close? Like "hey i pressed cancel close get back to the main window".

This is the full source of the second modification, in case you need:

#include <GuiConstants.au3>
GUICreate("Multiselezione by NATTA",300,230)
GUISetState(@SW_SHOW)
\$bottone1=GUICtrlCreateButton("Opzione 1",10,10)
GUICtrlCreateLabel("Aprire il notepad e scrivere l'ora e la data, poi chiuderlo"&@CRLF&"senza salvarlo",15,40)
\$bottone2=GUICtrlCreateButton("Opzione 2",10,80)
GUICtrlCreateLabel("Aprire la calcolatrice e eseguire un calcolo a scelta tra"&@CRLF&"2 operatori con le 4 operazioni di base",15,110)
\$bottone3=GUICtrlCreateButton("Opzione 3",10,150)
GUICtrlCreateLabel("Aprire il notepad, scrivere l'ora e la data, il sistema"&@CRLF&"operativo e chiudere senza salvare",15,180)

While 1
\$Scelta = GUIGetMsg()

If \$Scelta = \$GUI_EVENT_CLOSE Then ExitLoop

Select
Case \$Scelta=\$bottone1
BlockInput(1)
WinWaitActive("Senza nome - Blocco note")
Send("Ora e data "&@CRLF&"{F5}")
Sleep(1500)
WinClose("Senza nome - Blocco note")
WinWaitActive("Blocco note")
Send("!n")
BlockInput(0)
Case \$Scelta=\$bottone2
while Not IsNumber(\$operatore2) or \$operatore2<1 or \$operatore2>4
Msgbox(0, "", "Operatore errato")
WEnd
\$operatore1=InputBox("Inserire il primo operatore numerico","Primo Operatore:")
while Not IsNumber(\$operatore1)
Msgbox(0, "", "Operatore errato")
\$operatore1=InputBox("Inserire il primo operatore numerico","Primo Operatore:")
WEnd
\$operatore3=InputBox("Inserire il secondo operatore numerico","Secondo Operatore:")
while Not IsNumber(\$operatore3)
Msgbox(0, "", "Operatore errato")
\$operatore3=InputBox("Inserire il secondo operatore numerico","Secondo Operatore:")
WEnd
BlockInput(1)
Run("Calc.exe")
WinWaitActive("Calcolatrice")
Sleep(400)
Switch \$operatore2
Case 1
Send(\$operatore1&"*"&\$operatore3&"=")
Case 2
Send(\$operatore1&"/"&\$operatore3&"=")
Case 3
Send(\$operatore1&"+"&\$operatore3&"=")
Case 4
Send(\$operatore1&"-"&\$operatore3&"=")
EndSwitch
BlockInput(0)
Sleep(6000)
WinClose("Calcolatrice")
Case \$Scelta=\$bottone3
BlockInput(1)
WinWaitActive("Senza nome - Blocco note")
Send("Ora e data"&@CRLF&"{F5}"&@CRLF&"Sistema operativo"&@CRLF&@OSVersion)
Sleep(1500)
Winclose("Senza nome - Blocco note")
WinWaitActive("Blocco note")
Send("!n")
BlockInput(0)
EndSelect
WEnd

this is not working properly. Why?

Thanks for the help guys!

##### Share on other sites

There is a basic difference between "1" and 1 in AutoIt. The expression "1" is a string (ASCII code 0x31), while 1 is the actual number 1 (stored as a 32-bit integer 0x00000001).

An Input or Edit control will always return a string, not a number. So you are getting "20" (ASCII 0x32,0x30), not the number 20.

Most of the native AutoIt functions will convert "20" into 20 for you when you pass it to them, but IsNumber() tells you the unvarnished truth: "20" is not a number, but 20 is.

You can either convert with Number(), or test with StringIsInt() instead (see help file).

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
##### Share on other sites

Wow, one thing after the other.

When you read an input box (GUI) or input line (console), you read a string. So IsNumber("3") is false.

You should re-read the help file about InputBox (the help file says: Displays an input box to ask the user to enter a string.)

Now re-read language reference at datatypes and variables...

When you type "3", you get the character (element of a string) whose code is 0x33 hex. This is not a number but the string representation of a number.

Now try: \$n = Number("3") and then \$n contains the number 3 (0x00000003)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

Thanks to PsaltyDS and jchd for the very detailed explanation.

To MvGulik

Sorry if i have been too wordy

I read in the guide about leading type indicators. Are they of any use apart from aking easier knowing to a person reading the code understanding what type of variable he is reading? I mean, do autoit make something if i put \$iIntegerVar1=4 or \$IntegerVar=4??

Today is late and I'm going to code more tomorrow. I tried your solutions but they didn't worked. I'm considering to nuke this and start over. I'll let u know tomorrow.

If u feel, please answer to these questions or tell me if I can find something on the help, because i didn't.

Another question i have is that there is the possibility of restarting a loop like this withouth having to do multiple whiles like i had to do:

I wanted to do like this

If variable1 it's not a number then

Elseif

If variable 2 it's

ETC ETC...

While Variable 1 it's not a number

While End (Wend)

While....

which is a little longer. There isnt a way to say autoit to restart the loop if the condition isn't verified.

Other question is:

When i click cancel in one of those Inputboxes, it keeps coming back. There is a way to make it close? Like "hey i pressed cancel close get back to the main window".

Edited by niubbone
##### Share on other sites

AutoIt doesn't care how you call your variables, as long as their name is valid variable syntax. Using type prefixes is only an help and a remainder for the programmer. Nobody cares except you.

Here's a simple working loop:

Local \$input
While 1
\$input = InputBox("Check input", "Enter an integer value")
If @error Then ExitLoop
If Not StringIsInt(\$input) Then
MsgBox(0, "Error", "You did NOT enter an integer!")
ContinueLoop
Else
MsgBox(0, "Success", "You entered " & \$input & @LF & "(which is the same as " & Number(\$input) & ")")
EndIf
WEnd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

Thanks I solved. The problem was around putting just one while loop at beginning including the whole code, then putting an Exitloop at the end of the code (or it keeps asking for the 3 variables forever), and the Wend.

here's the code:

Case \$Scelta=\$bottone2
While 1
While 1
If @error Then ExitLoop
If Not StringIsInt(\$operatore2) or \$operatore2<1 or \$operatore2>4 Then
Msgbox(0, "", "Operatore errato")
ContinueLoop
Endif
ExitLoop
Wend
While 1
\$operatore1=InputBox("Inserire il primo operatore numerico","Primo Operatore:")
If @error Then ExitLoop
If Not StringIsInt(\$operatore1) Then
Msgbox(0, "", "Operatore errato")
ContinueLoop
Endif
ExitLoop
Wend
While 1
\$operatore3=InputBox("Inserire il primo operatore numerico","Secondo Operatore:")
If @error Then ExitLoop
If Not StringIsInt(\$operatore3) Then
Msgbox(0, "", "Operatore errato")
ContinueLoop
Endif
ExitLoop
Wend
BlockInput(1)
Run("Calc.exe")
WinWaitActive("Calcolatrice")
Sleep(400)
Switch \$operatore2
Case 1
Send(\$operatore1&"*"&\$operatore3&"=")
Case 2
Send(\$operatore1&"/"&\$operatore3&"=")
Case 3
Send(\$operatore1&"+"&\$operatore3&"=")
Case 4
Send(\$operatore1&"-"&\$operatore3&"=")
EndSwitch
BlockInput(0)
Sleep(6000)
WinClose("Calcolatrice")
ExitLoop
WEnd

By the way I was wondering what's for If @error.

What @error does exactly here and more in general? I checked the help documentation and it's not very clear...

I didn't solved the cancel button thing (getting back to the main gui), when pressing cancel with the code up there

Also i would like to ask why Continueloop is necessary there (otherwise the program won't work properly, I tried.

Another thing I'm going to try is to put some while loops in middle of the main one, because if i put a wrong value in the inputbox, it restart from the 1st one. For example when i type 3ffd in the inputbox "Secondo Operatore" it restart from the beginning (the one asking for 1,2,3,4)

NEVERMIND, I UPDATED THE CODE, THE WHILES ARE THERE WORKING FINE.

Just added a While 1, ExitLoop, Wend to each step.

Last question: Is that code fine for you? Would you have coded it differently or in a more clever/space saving way?

Thanks again for the support, my brain hurted yesterday

Edited by niubbone
##### Share on other sites

What exactly do you find unclear regarding @error in the InputBox documentation?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

From the help:

@error Status of the error flag. See the SetError function.

_________________

SetError

Manually set the value of the @error macro.

SetError ( code [, extended [, return value]] )

Parameters

code The required value (integer) to set the @error macro to.

extended [optional] The optional value (integer) to set the @extended macro to. This sets the same macro as the SetExtended() function.

return value [optional] Override the default return value and return this parameter.

I don't even get how i sould set an error, and how it should work. Plus in that function, we didn't assigned and SetError Function...

Plus:

Return Value

By default, none, however if the optional return value argument is passed, then the function will return that value.

I don't get how this works...

##### Share on other sites

My question was: "What exactly do you find unclear regarding @error in the InputBox documentation?"

As you've just read, most functions set a system-wide variable, @error, to denote error condition that may have occured. Some set another system-wide variable, @extended as well.

After invoking a function likely to fail (e.g. I/O functions) it's a good idea to check for error.

It's important to check @error after InputBox because it's the only way to know that the user is fed up playing with the program and wants to close the thing. All this is explained in full in the help file but it requires that you read it.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

My question was: "What exactly do you find unclear regarding @error in the InputBox documentation?"

As you've just read, most functions set a system-wide variable, @error, to denote error condition that may have occured. Some set another system-wide variable, @extended as well.

After invoking a function likely to fail (e.g. I/O functions) it's a good idea to check for error.

It's important to check @error after InputBox because it's the only way to know that the user is fed up playing with the program and wants to close the thing. All this is explained in full in the help file but it requires that you read it.

Thanks

There is a way to make that if the user press cancel the programs get back to the main gui?

I've seen in help that @error=1 if cancel is pressed. I think it will be sufficient a If @error=1 Exitloop right? Or a If @error>1 is better?

Nevermind I tried some by myself.

The new problem is that if I pick choice 3 (Sum) and I input 33 as first operator, and 33 as second operator, the program just types in the windows calculator 333, while the code to send to windows calculator is: Send(\$operatore1&"+"&\$operatore3&"=")

How this is possible? I mean, I could understand bad manipulated variables, but there is a "+" and a "=" sent there as strings, unrelated to any other variable.... The correct keystrokes would be 33+33=, and also if the second operator it's changed into s single 3 it should be 33+3=. Instead, autoit doesn't types even the +...

I thought it was because autoit typed that too fast (also if there is WinWaitActive) so i put in the code AutoItSetOption("SendKeyDelay", 400) to be sure but the result it's the same.... Why this? I'm attaching the full source so you can check it

#include <GuiConstants.au3>
GUICreate("Multiselezione by NATTA",300,230)
GUISetState(@SW_SHOW)
\$bottone1=GUICtrlCreateButton("Opzione 1",10,10)
GUICtrlCreateLabel("Aprire il notepad e scrivere l'ora e la data, poi chiuderlo"&@CRLF&"senza salvarlo",15,40)
\$bottone2=GUICtrlCreateButton("Opzione 2",10,80)
GUICtrlCreateLabel("Aprire la calcolatrice e eseguire un calcolo a scelta tra"&@CRLF&"2 operatori con le 4 operazioni di base",15,110)
\$bottone3=GUICtrlCreateButton("Opzione 3",10,150)
GUICtrlCreateLabel("Aprire il notepad, scrivere l'ora e la data, il sistema"&@CRLF&"operativo e chiudere senza salvare",15,180)

While 1
\$Scelta = GUIGetMsg()

If \$Scelta = \$GUI_EVENT_CLOSE Then ExitLoop

Select
Case \$Scelta=\$bottone1
BlockInput(1)
WinWaitActive("Senza nome - Blocco note")
Send("Ora e data "&@CRLF&"{F5}")
Sleep(1500)
WinClose("Senza nome - Blocco note")
WinWaitActive("Blocco note")
Send("!n")
BlockInput(0)
Case \$Scelta=\$bottone2
While 1
While 1
If @error Then ExitLoop
If Not StringIsInt(\$operatore2) or \$operatore2<1 or \$operatore2>4 Then
Msgbox(0, "Errore", "Non hai inserito un numero intero")
ContinueLoop
Endif
ExitLoop
Wend
While 1
\$operatore1=InputBox("Inserire il primo operatore numerico","Primo Operatore:")
If @error Then ExitLoop
If Not StringIsInt(\$operatore1) Then
Msgbox(0, "Errore", "Non hai inserito un numero intero")
ContinueLoop
Endif
ExitLoop
Wend
While 1
\$operatore3=InputBox("Inserire il primo operatore numerico","Secondo Operatore:")
If @error Then ExitLoop
If Not StringIsInt(\$operatore3) Then
Msgbox(0, "Errore", "Non hai inserito un numero intero")
ContinueLoop
Endif
ExitLoop
Wend
BlockInput(1)
Run("Calc.exe")
WinWaitActive("Calcolatrice")
AutoItSetOption("SendKeyDelay",400)
Switch \$operatore2
Case 1
Send(\$operatore1&"*"&\$operatore3&"=")
Case 2
Send(\$operatore1&"/"&\$operatore3&"=")
Case 3
Send(\$operatore1&"+"&\$operatore3&"=")
Case 4
Send(\$operatore1&"-"&\$operatore3&"=")
EndSwitch
BlockInput(0)
Sleep(6000)
WinClose("Calcolatrice")
ExitLoop
WEnd
Case \$Scelta=\$bottone3
BlockInput(1)
WinWaitActive("Senza nome - Blocco note")
Send("Ora e data"&@CRLF&"{F5}"&@CRLF&"Sistema operativo"&@CRLF&@OSVersion)
Sleep(1500)
Winclose("Senza nome - Blocco note")
WinWaitActive("Blocco note")
Send("!n")
BlockInput(0)
EndSelect
WEnd
Edited by niubbone

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...