problem with fields/math

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 332
Joined: Sun Apr 15, 2012 1:17 am
Contact:

problem with fields/math

Post by Newbie4 » Thu Sep 20, 2012 9:16 pm

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?
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: problem with fields/math

Post by bn » Thu Sep 20, 2012 9:35 pm

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

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: problem with fields/math

Post by Klaus » Thu Sep 20, 2012 9:39 pm

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

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 332
Joined: Sun Apr 15, 2012 1:17 am
Contact:

Re: problem with fields/math

Post by Newbie4 » Thu Sep 20, 2012 9:54 pm

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
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: problem with fields/math

Post by mwieder » Thu Sep 20, 2012 10:02 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10324
Joined: Wed May 06, 2009 2:28 pm

Re: problem with fields/math

Post by dunbarx » Fri Sep 21, 2012 4:14 am

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

Post Reply