Page 1 of 1

problem with fields/math

Posted: Thu Sep 20, 2012 9:16 pm
by Newbie4
I am trying to calculate the area of a rectangle after the user types in the length and width. I have 3 text input fields: "length", "width" and "area". The user puts in the length and width, then presses the "calculate" button which has the following code:

Code: Select all

global l
global w
on mouseUp
   put field "width" into w
   put field "length" into l
   put l*w into field "area"  --line 6
end mouseUp
It works the first time, then I keep getting this error:
button "Calculate": execution error at line 6 (Operators *: error in left operand), char 8
What am I missing? I tried different variable names, parentheses, etc. It has to be something stupid I am doing wrong or that I am missing. Can anyone help me?

Re: problem with fields/math

Posted: Thu Sep 20, 2012 9:35 pm
by bn
Hi Newbie4,

if I try your script in a new stack with the fields you indicate and a button and your script I can hit the button as often as I want and no error occurs. This leads me to believe that the globals l and w are filled/altered/emptied in other scripts.
You don't have to make l and w global variables for the sake of this script. In fact it opens the script to errors. Except you need the global varibles l and w later in other places. Then you could use a global, but you have to be careful what you put into them and when. This is all assuming that the fields don't change between clicking the button.

Your script works just a well if you just omit the global declaration on top of the script.

Kind regards
Bernd

Re: problem with fields/math

Posted: Thu Sep 20, 2012 9:39 pm
by Klaus
Hi,

the script is definitively OK, so there may be one or more invisible "characters" in the field?
Let you "answer l" and "answer w" or anything to check the strings in the fields.


Best

Klaus

Re: problem with fields/math

Posted: Thu Sep 20, 2012 9:54 pm
by Newbie4
Thanks to both of you. The answer did the trick. There were numbers, each followed by a "return" which is what killed the calculation (l*w)

Force of habit, pressing the "enter" key after entering something in a field.

Checking the "Tab on Return" and the "Don't Wrap" will solve this problem in the future.

Thanks again

Re: problem with fields/math

Posted: Thu Sep 20, 2012 10:02 pm
by mwieder
To prevent that you can say

Code: Select all

put word 1 to -1 of field "width" into w
and it will strip out the returns

Re: problem with fields/math

Posted: Fri Sep 21, 2012 4:14 am
by dunbarx
Hi.

After a while you will learn, from the nature of the error, where to look for the problem. in this case it is an oldie, that the calculation was being asked to add, say, 3a to 6. The fact that the offending character was invisible is just the way life sometimes is.

If you are adventurous, do you think you could write a function that prevents this error? This is a valuable lesson, as data validation is required all the time, for dates, numbers, etc.

Try this, Write back for hints or sympathy.

Craig Newman