Page 1 of 1

How to create scripts for buttons that execute arithmetic operations

Posted: Thu May 23, 2024 10:28 am
by tetroup
Hello, LiveCode specialists.

I'm working on a project that includes constructing a simple calculator app with LiveCode. I followed the tutorial and successfully created a basic interface with buttons and fields. However, I am unsure how to build the script for the buttons to conduct the arithmetic calculations. I looked through the LiveCode docs and forums, but I couldn't find a clear example or explanation for how to achieve this.

How can I create a script for the calculator buttons that performs addition, subtraction, multiplication, and division on the numbers entered into the fields? What LiveCode commands and syntax do I need to use? How do I deal with problems like dividing by zero or entering invalid input?

I would appreciate any assistance or advice on how to resolve this issue.

Re: How to create scripts for buttons that execute arithmetic operations

Posted: Thu May 23, 2024 3:18 pm
by dunbarx
Hi, Welcome to LC and this forum.

Here is a simple stack. Check out the scripts of the "+" button, the "clear" button and the two top fields. This should be enough to finish your app.
simpleAdder.livecode.zip
(1.1 KiB) Downloaded 628 times
Craig

Re: How to create scripts for buttons that execute arithmetic operations

Posted: Thu May 23, 2024 3:22 pm
by dunbarx
Just read your post again.

When you have mastered the sample stack, the answer to your other questions will become pertinent. Write back at that time and we can go through the MANY small tweaks that a reliable calculator should have, like entry validation. It would not be good if the user tried to add 42 to "chicken".

Craig

Re: How to create scripts for buttons that execute arithmetic operations

Posted: Thu May 23, 2024 3:33 pm
by dunbarx
Me again. I am not sure how your app is built. For example, if it contains only buttons with numbers labelled on them, and other buttons that only, say, add, there is no need for entry validation. But you yourself raised that point, so perhaps your calculator asks the user to enter numbers, and then the issue could arise.

All this is to make you think about what you are building, and how it will work, entirely apart from the tools in LC you need to make it happen.

Craig

Re: How to create scripts for buttons that execute arithmetic operations

Posted: Thu May 23, 2024 4:58 pm
by FourthWorld
tetroup wrote:
Thu May 23, 2024 10:28 am
I'm working on a project that includes constructing a simple calculator app with LiveCode. I followed the tutorial and successfully created a basic interface with buttons and fields. However, I am unsure how to build the script for the buttons to conduct the arithmetic calculations.
There are dozens of tutorials. Did you see the one for making a calculator?
https://livecode.com/lessons/calculator/

Re: How to create scripts for buttons that execute arithmetic operations

Posted: Thu May 23, 2024 5:04 pm
by dunbarx
Richard.

The OP mentioned following a tutorial, though not which one. I suspect that since entry validation was mentioned early on, that the "calculator" is more home-brewed than the ordinary tutorial version.

Craig