How to create scripts for buttons that execute arithmetic operations

The place to discuss anything and everything about running your LiveCode on Android

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
tetroup
Posts: 1
Joined: Thu May 23, 2024 10:26 am
Contact:

How to create scripts for buttons that execute arithmetic operations

Post by tetroup » Thu May 23, 2024 10:28 am

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9785
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Thu May 23, 2024 3:18 pm

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 40 times
Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9785
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Thu May 23, 2024 3:22 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9785
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Thu May 23, 2024 3:33 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9867
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

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

Post by FourthWorld » Thu May 23, 2024 4:58 pm

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/
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9785
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Thu May 23, 2024 5:04 pm

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

Post Reply

Return to “Android Deployment”