I CANNOT DO A SUM PLEASE HELP
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 1
- Joined: Mon Oct 23, 2017 3:10 am
I CANNOT DO A SUM PLEASE HELP
i have two variables, pizzaSize and pizzaIng. according to some buttons I have, they were given values. I have another button that supposedly sums the values in those two variables. The thing is, it marks an error in line 4, and I have tried everything and I just can't get it to do a simple sum and later add it to another variable. WHAT CAN I DO?
Re: I CANNOT DO A SUM PLEASE HELP
I see in the picture that you have a tab in the editor open for button "2", so whether button "Submit" would see the information would depend on how you set up the variables. If, for instance, button "2" puts the information into a variable local only to button "2", button "Submit" won't see that.ricardorcesar wrote: ↑Mon Oct 23, 2017 3:21 ami have two variables, pizzaSize and pizzaIng. according to some buttons I have, they were given values.
There are a few ways to do something like this, probably the easiest with the least change in your existing code (but least elegant) would be to use a field set to not visible to store the values instead of a variable, then add the fields contents. Other options would include using a global variable, using a custom property, or putting the code of the other buttons at card level instead of each buttons level and using if target = button "x" then statements.
Here is a link to a simple calculator that demonstrates some of the things you can do with numbers. You should probably be able to complete all the things demonstrated within an hour.
-
- VIP Livecode Opensource Backer
- Posts: 9665
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: I CANNOT DO A SUM PLEASE HELP
Hi.
First off, in line 2 you "get" the variable "pizzaSize". So the local variable "it" now has "pizzaSize". But then you immediately place the variable "pizzalng" into "it", replacing the value just loaded.
Then in line 4 you "get" the sum of those two variables. Well and good, but this makes lines 2 and 3 irrelevant, though there is nothing intrinsically wrong about it all.
Now to the actual problem. If LC is throwing an error in line 4, it means that one or both of those two arguments (the values of the two variables) are not numbers. Place a breakpoint just after your current line 3, and look at the values of those variables. Make sure you really see what those variables contain, because invisible characters can also create problems when LC tries to evaluate them as simple numbers. Foe example, if you see that "pizzaSize" seems to contain a "5", and that seems fine, make sure that the length of pizzaSize is 1.
Write back if you are still stuck. Everyone finds themselves in seemingly intractable simple quagmires. The solution almost always lies somewhere with the programmer. You can help yourself greatly by validating user input, for example:
That sort of thing.
Craig Newman
First off, in line 2 you "get" the variable "pizzaSize". So the local variable "it" now has "pizzaSize". But then you immediately place the variable "pizzalng" into "it", replacing the value just loaded.
Then in line 4 you "get" the sum of those two variables. Well and good, but this makes lines 2 and 3 irrelevant, though there is nothing intrinsically wrong about it all.
Now to the actual problem. If LC is throwing an error in line 4, it means that one or both of those two arguments (the values of the two variables) are not numbers. Place a breakpoint just after your current line 3, and look at the values of those variables. Make sure you really see what those variables contain, because invisible characters can also create problems when LC tries to evaluate them as simple numbers. Foe example, if you see that "pizzaSize" seems to contain a "5", and that seems fine, make sure that the length of pizzaSize is 1.
Write back if you are still stuck. Everyone finds themselves in seemingly intractable simple quagmires. The solution almost always lies somewhere with the programmer. You can help yourself greatly by validating user input, for example:
Code: Select all
ask "enter a size"
if it is a number then put it into pizzaSize
else answer "Bad data"
Craig Newman
Re: I CANNOT DO A SUM PLEASE HELP
Hi ricardo,
1. welcome to the forum!
2. Do not use ALL CAPS for the thread title.
Everyone should no these days that this is considered SCREAMING on the internet, which is highly impolite.
3. And please do not add something like "I NEED HELP", a forum is menat to help other people so this is really not neccessary.
4. To learn the basics of Livecode, please work throught these stacks:
http://www.hyperactivesw.com/revscriptc ... ences.html
Best
Klaus
1. welcome to the forum!
2. Do not use ALL CAPS for the thread title.
Everyone should no these days that this is considered SCREAMING on the internet, which is highly impolite.
3. And please do not add something like "I NEED HELP", a forum is menat to help other people so this is really not neccessary.
4. To learn the basics of Livecode, please work throught these stacks:
http://www.hyperactivesw.com/revscriptc ... ences.html
Best
Klaus
Re: I CANNOT DO A SUM PLEASE HELP
Probably you want this:ricardorcesar wrote: ↑Mon Oct 23, 2017 3:21 ami have two variables, pizzaSize and pizzaIng. according to some buttons I have, they were given values. I have another button that supposedly sums the values in those two variables. The thing is, it marks an error in line 4, and I have tried everything and I just can't get it to do a simple sum and later add it to another variable. WHAT CAN I DO?
Screen Shot 2017-10-22 at 8.47.53 PM.png
########CODE to copy and paste#######
on mouseUp
#SUM
put field "pizzaSize" + field "pizzaIng" into PriceTotal
put empty into field "pizzaSize"
put empty into field "pizzaIng"
#String concatenation
put field "receiptSize" & field "receiptIng" into field "Recipt"
put empty into field "receiptSize"
put empty into field "receiptIng"
end mouseUp
#####END OF CODE generated by http://tinyurl.com/j8xf3xq with livecode 9.0.0-dp-6#####
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
Re: I CANNOT DO A SUM PLEASE HELP
You've gotten good suggestions.
I think your original error on line 4 is because the pizzaSize and pizzaIng variables have nothing in them.
Try putting this at the top of all scripts where you use those variables:
Jim Lambert
I think your original error on line 4 is because the pizzaSize and pizzaIng variables have nothing in them.
Try putting this at the top of all scripts where you use those variables:
Code: Select all
global pizzaSize, pizzaIng
Re: I CANNOT DO A SUM PLEASE HELP
To be precise, variables that have not been initialized (means put data into them) have their name as string as content!
Check this in the message box:
answer newvariablewithoutcontent -> newvariablewithoutcontent
So the error comes from the fact that the enigne tries to sum up two STRINGS.
Re: I CANNOT DO A SUM PLEASE HELP
Genau!
Klaus is exactly correct.
JimL
Klaus is exactly correct.
JimL