Page 1 of 1

I CANNOT DO A SUM PLEASE HELP

Posted: Mon Oct 23, 2017 3:21 am
by ricardorcesar
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?
Screen Shot 2017-10-22 at 8.47.53 PM.png

Re: I CANNOT DO A SUM PLEASE HELP

Posted: Mon Oct 23, 2017 4:28 am
by bogs
ricardorcesar wrote:
Mon Oct 23, 2017 3:21 am
i have two variables, pizzaSize and pizzaIng. according to some buttons I have, they were given values.
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.

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.

Re: I CANNOT DO A SUM PLEASE HELP

Posted: Mon Oct 23, 2017 6:18 am
by dunbarx
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:

Code: Select all

ask "enter a size"
  if it is a number then put it into pizzaSize
  else answer "Bad data"
That sort of thing.

Craig Newman

Re: I CANNOT DO A SUM PLEASE HELP

Posted: Mon Oct 23, 2017 10:00 am
by Klaus
Hi ricardo,

1. welcome to the forum! :D

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

Posted: Mon Oct 23, 2017 12:22 pm
by MaxV
ricardorcesar wrote:
Mon Oct 23, 2017 3:21 am
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?

Screen Shot 2017-10-22 at 8.47.53 PM.png
Probably you want this:
########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#####

Re: I CANNOT DO A SUM PLEASE HELP

Posted: Mon Oct 23, 2017 6:44 pm
by jiml
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:

Code: Select all

global pizzaSize, pizzaIng
Jim Lambert

Re: I CANNOT DO A SUM PLEASE HELP

Posted: Mon Oct 23, 2017 7:17 pm
by bogs
jiml wrote:
Mon Oct 23, 2017 6:44 pm
I think your original error on line 4 is because the pizzaSize and pizzaIng variables have nothing in them.
Thank you, much better way of saying what I overstated :)

Re: I CANNOT DO A SUM PLEASE HELP

Posted: Mon Oct 23, 2017 7:40 pm
by Klaus
bogs wrote:
Mon Oct 23, 2017 7:17 pm
jiml wrote:
Mon Oct 23, 2017 6:44 pm
I think your original error on line 4 is because the pizzaSize and pizzaIng variables have nothing in them.
Thank you, much better way of saying what I overstated :)
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

Posted: Tue Oct 24, 2017 5:16 pm
by jiml
Genau!
Klaus is exactly correct.

JimL