code 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: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
Gytoman
Posts: 3
Joined: Tue Mar 15, 2016 12:11 pm

code help

Post by Gytoman » Tue Mar 15, 2016 12:31 pm

The file is attached, takes in 9 names and match attendance. I can't get it to output to = output 1, and output 2 error at line 51. button "Button 1": execution error at line 51 (Chunk: error in range start expression), char 1. Anyone like to help me, please. Here is the code for this attempt.

global nameOfperson, gigsGone, calc, money, ticket?, winner1, winner2

on mouseUp
declareVariables
repeat with index=1 to 9
nameOf
NoOfGigs
calculateLoyalFanness
finalOutput
end repeat
end mouseUp

on declareVariables //Declareing variables and emptying field
put "" into nameOfperson
put 0 into NoOfGigs
put empty into field "output 1"
put empty into field "output 2"
put 1 into index
end declareVariables

on nameOf //Their information
ask "What is your first name?"
if the result = "Cancel" then exit to top
put it into nameOfperson[index]
end nameOf

on NoOfGigs //The number of gigs they have been to
ask "How many of their gigs have you been to?"
if the result = "Cancel" then exit to top
put it into gigsGone[index]
if gigsGone[index] >2 and gigsGone[index] <6 and gigsGone[index] is an integer then
else
answer "Sorry, that number of gigs is not viable for loyal fandom."
end if
end NoOfGigs

on calculateLoyalFanness //Calculate the price of thier ticket
if gigsGone[index] = 3 then
put 0.7 into calc
end if
if gigsGone[index] = 4 then
put 0.6 into calc
end if
if gigsGone[index] = 5 then
put 0.5 into calc
end if
put 15 * calc into money[index]
end calculateLoyalFanness

on finalOutput //Output the information that really matters
put nameOfperson[index]& "the cost of your final concert ticket with the appropriate discount is £"&money[index]& "" into line index of field "output 1"
//put ""&nameOfperson[index]&"the cost of your final concert ticket with the appropriate discount is £" &money[index]& "" into line index of field "output 1"
repeat until winner1 <> winner2
put random(9) into winner1
put random(9) into winner2
end repeat
put nameOfperson[winner1] into line 1 of field "output 2"
put nameOfperson[winner2] into line 2 of field "output 2"
end finalOutput

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

Re: code help

Post by dunbarx » Tue Mar 15, 2016 1:50 pm

Hi.

Step through your handlers. The line that fails (first line in the handler "finalOutput) is asking you to put something into "line index" of a field. But "index" as a variable name does not contain a number, the only chunk that will properly resolve for the parser to work properly.

These sorts of issues are easily fixed by stepping through.

Craig Newman

Gytoman
Posts: 3
Joined: Tue Mar 15, 2016 12:11 pm

Re: code help

Post by Gytoman » Tue Mar 15, 2016 2:10 pm

I am very new to this, sorry, but can you be a little more specific? Do I need the declare Index as a Variable, Integer? What is needed as a number in Index?

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

Re: code help

Post by dunbarx » Tue Mar 15, 2016 3:32 pm

OK.

First off, a very nice early project. I like the way you structure your script, with handlers called from the main "mouseUp" routine. You should have fun with this.

Know that when you re-use variables among handlers in the same script, you must declare them as local in that script:

Code: Select all

local nameOfPerson,money,gigsGone,index

on mouseUp
....
Otherwise, subsequent handlers will not know what you are talking about. If you want these to be available in other scripts in other objects, you must declare them as global. It is possible to pass values as parameters between handlers, but we will leave that for when you are more comfortable. And custom properties are my preferred method of storing data universally, but that also is for a little later.

One caveat: I would not use the same variable name in multiple places, unless you really want to, as when you pass parameters. Your "index" is an ordinary variable, an "index" for a repeat loop, and an array element name. Just don't.

Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”