Page 1 of 1

How to perform basic arithmetic operations

Posted: Mon Feb 26, 2024 5:41 am
by SheltonCruz
I'm trying to create a simple calculator app with LiveCode, but I'm stuck on how to implement the basic arithmetic operations. I have created a stack with four buttons for addition, subtraction, multiplication, and division, and two fields for entering and displaying the numbers. I have also written some scripts for each button, but they don't seem to work properly. Here is an example of my script for the addition button:

on mouseUp
put field "number1" into x
put field "number2" into y
put x + y into field "result"
end mouseUp

When I click the button, nothing happens in the result field. I have checked the syntax and the variable names, but I can't find any errors. Can anyone please help me figure out what I'm doing wrong and how to fix it?

Re: How to perform basic arithmetic operations

Posted: Mon Feb 26, 2024 9:13 am
by richmond62
Dunno: works perfectly round these parts . . .
-
SShot 2024-02-26 at 10.12.03.png
-
Either there is something else wrong, or you are just 'trailing your coat' for a completely different reason.

Re: How to perform basic arithmetic operations

Posted: Mon Feb 26, 2024 10:19 am
by SparkOut
Things to check (and have the script do some of the error checking for you):

Field names match the script references

Field contents are a number
(For example, not a blank line that's been pushed out of view at the top of the field because of a number sitting in view at the bottom)
Make the script check

Code: Select all

if x is a number and y is a number then
  put x + y into field "result"
else
  answer "x is" && x &&  "and y is" && "These are not both valid numbers"
end if

Re: How to perform basic arithmetic operations

Posted: Mon Feb 26, 2024 12:22 pm
by stam
SparkOut wrote:
Mon Feb 26, 2024 10:19 am
Make the script check

Code: Select all

if x is a number and y is a number then
 
Alternatively, or perhaps complementing this, especially since this is an app designed as numerical calculator (presumably), the fields themselves should not allow any data entry that isn't a number. In the field's script, only allow keyDown if the the key pressed is a number or something like a comma or period:

Code: Select all

on keyDown pKeyName
    if pKeyName is in "0123456789,." then pass keyDown
end keyDown
The field then will only allow digits or "." or "," to be entered (edit as needed). With this, the text of the field can only ever contain numbers.
Of course, safest is to do both this and what Sparkout says, if you want the calculator logic to apply to inputs other than the field where you control the input...

Re: How to perform basic arithmetic operations

Posted: Mon Feb 26, 2024 3:21 pm
by dunbarx
What everyone is saying is that there is something structural wrong with your stack, and probably not particularly with your code. For a new user, such traps live there, as opposed to your thinking.

Here is a simple stack that does what you say, at least with addition. Can you find what is different in your own stack?

Craig
simpleArithmetic.livecode.zip
(1013 Bytes) Downloaded 23 times

Re: How to perform basic arithmetic operations

Posted: Mon Feb 26, 2024 3:23 pm
by dunbarx
I see Richmond has uploaded what looks like, apart from the backGround color, what I did.

Craig

Re: How to perform basic arithmetic operations

Posted: Mon Feb 26, 2024 4:49 pm
by Randy Hengst
Given that I’ve made this mistake myself, double-check to make sure you have only one field each with the names “number1”, “number2” “result”

take care,
Randy

Re: How to perform basic arithmetic operations

Posted: Mon Feb 26, 2024 6:24 pm
by dunbarx
hi.

Once we get this little problem straightened out, and with entry validation others have mentioned, how are you handling the display of the number of allowed decimal points? Might not this need to be adjustable by the user? Will you need special handling for very large numbers?

Is this getting less and less simple?

Craig

Re: How to perform basic arithmetic operations

Posted: Mon Feb 26, 2024 8:11 pm
by SparkOut
stam wrote:
Mon Feb 26, 2024 12:22 pm
The field then will only allow digits or "." or "," to be entered (edit as needed). With this, the text of the field can only ever contain numbers.
Of course, safest is to do both this and what Sparkout says, if you want the calculator logic to apply to inputs other than the field where you control the input...
This is very much in my wheelhouse. I am big on data validation, but input validation/restrictions even more. If you need to input only numbers, then using a dial or slider can be even better than using a field even with limited inputs. Or using a radio button or checkbox group to choose from a limited set of choices.
But this is moving still further away from the OP's basic task.

Re: How to perform basic arithmetic operations

Posted: Mon Feb 26, 2024 9:06 pm
by dunbarx
Sparkout.
If you need to input only numbers, then using a dial or slider can be even better than using a field even with limited inputs
How can you input numbers for a calculator using a slider?? How would you enter "27.25 + 55.19" ?

Craig

Re: How to perform basic arithmetic operations

Posted: Mon Feb 26, 2024 9:16 pm
by stam
Actually a slider by default will produce float/decimal numbers
Still a pain tho, and not something I would recommend unless you want unhappy users!

Since this is a calculator it makes sense to have buttons that will also click when you type the number (and decimal point), not sure I’ve ever seen a calculator with sliders!
But I guess there’s always a first time lol

Re: How to perform basic arithmetic operations

Posted: Mon Feb 26, 2024 10:06 pm
by dunbarx
Stam, Sparkout.

I love and use sliders.

But not when three, four or seven significant figures AND with a decimal point located somewhere in the mix, must be selected with that sort of gadget. A keypad is really nice...

Craig

Re: How to perform basic arithmetic operations

Posted: Mon Feb 26, 2024 10:16 pm
by SparkOut
I should have forced myself to carry on phonetyping and say "but it depends on the user interface" and "try to make the UX intuitive and simple/sensible".
Slider values can be rounded or constrained quite simply.
For a calculator UI, uh... use buttons with a big numeric (or delete, function,etc) icon that populates a field that the user doesn't get to edit directly. (Your keypad)
But the point is "try to prevent erroneous input by making sensible UI choices" (and validate what does become the input data)

Re: How to perform basic arithmetic operations

Posted: Tue Feb 27, 2024 3:04 pm
by dunbarx
Sparkout.

And I should have realized that your idea did not require that, say, if one wanted to add 35.7 to 14.22, the slider would NOT have to find those exact values in its travels, but rather select individual digits in succession. My point was not well taken.

But even done the "right" way, though doable, is MUCH more tedious than simply typing, as you point out.

And done that right way, how would the user select the current slider value? Hit the enter key?

Craig