How to perform basic arithmetic operations
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 1
- Joined: Mon Feb 26, 2024 5:34 am
- Contact:
How to perform basic arithmetic operations
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?
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?
-
- Livecode Opensource Backer
- Posts: 9446
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: How to perform basic arithmetic operations
Dunno: works perfectly round these parts . . .
- -
Either there is something else wrong, or you are just 'trailing your coat' for a completely different reason.
- -
Either there is something else wrong, or you are just 'trailing your coat' for a completely different reason.
- Attachments
-
- example.livecode.zip
- (994 Bytes) Downloaded 24 times
Re: How to perform basic arithmetic operations
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
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
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:SparkOut wrote: ↑Mon Feb 26, 2024 10:19 amMake the script checkCode: Select all
if x is a number and y is a number then
Code: Select all
on keyDown pKeyName
if pKeyName is in "0123456789,." then pass keyDown
end keyDown
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...
-
- VIP Livecode Opensource Backer
- Posts: 9731
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How to perform basic arithmetic operations
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
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
Last edited by dunbarx on Mon Feb 26, 2024 5:17 pm, edited 1 time in total.
-
- VIP Livecode Opensource Backer
- Posts: 9731
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How to perform basic arithmetic operations
I see Richmond has uploaded what looks like, apart from the backGround color, what I did.
Craig
Craig
-
- VIP Livecode Opensource Backer
- Posts: 154
- Joined: Thu Jun 29, 2006 4:16 pm
Re: How to perform basic arithmetic operations
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
take care,
Randy
-
- VIP Livecode Opensource Backer
- Posts: 9731
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How to perform basic arithmetic operations
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
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
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.stam wrote: ↑Mon Feb 26, 2024 12:22 pmThe 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...
But this is moving still further away from the OP's basic task.
-
- VIP Livecode Opensource Backer
- Posts: 9731
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How to perform basic arithmetic operations
Sparkout.
Craig
How can you input numbers for a calculator using a slider?? How would you enter "27.25 + 55.19" ?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
Craig
Re: How to perform basic arithmetic operations
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9731
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How to perform basic arithmetic operations
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
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
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)
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)
-
- VIP Livecode Opensource Backer
- Posts: 9731
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How to perform basic arithmetic operations
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
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