I CANNOT DO A SUM PLEASE HELP

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
ricardorcesar
Posts: 1
Joined: Mon Oct 23, 2017 3:10 am

I CANNOT DO A SUM PLEASE HELP

Post by ricardorcesar » 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

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: I CANNOT DO A SUM PLEASE HELP

Post by bogs » Mon Oct 23, 2017 4:28 am

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.
Image

dunbarx
VIP Livecode Opensource Backer
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

Post by dunbarx » Mon Oct 23, 2017 6:18 am

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

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: I CANNOT DO A SUM PLEASE HELP

Post by Klaus » Mon Oct 23, 2017 10:00 am

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

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: I CANNOT DO A SUM PLEASE HELP

Post by MaxV » Mon Oct 23, 2017 12:22 pm

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#####
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: I CANNOT DO A SUM PLEASE HELP

Post by jiml » Mon Oct 23, 2017 6:44 pm

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

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: I CANNOT DO A SUM PLEASE HELP

Post by bogs » 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 :)
Image

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: I CANNOT DO A SUM PLEASE HELP

Post by Klaus » Mon Oct 23, 2017 7:40 pm

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.

jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: I CANNOT DO A SUM PLEASE HELP

Post by jiml » Tue Oct 24, 2017 5:16 pm

Genau!
Klaus is exactly correct.

JimL

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”