Trouble with some code

I am getting return values of 0 in both text boxes. Where am I going wrong.

nFacets = val(txtTotalParts.text) -1 * val(txtTotalBights.text) nHalfCycles = val(txtTotalBights.text) *2

Return value code;

txtTotalFacets.text = str(nFacets) txtTotalHalfCycles.text = str(nHalfCycles)

I am also having a bit trouble with getting the GCD to work correctly.

[code]
dim A , B, nGCD as integer

//nGCD
if nBights > nParts then
A = nBights
B = nParts
else
A = nParts
B = nBights
end if

'do
'nParts =a mod b
'a = b

'b = nBights
'loop until B=0
nGCD = A
nGCD =nParts mod nBights

//now display the results by setting text fields .text property

txtTotalGCD.text = str(nGCD)[/code]

Looks ok. There could be many reasons why you are getting a 0 in your (I assume) textbox.

Sprinkle around some debug messages i.e.

system.debuglog(“nFacets=” + str(nFacets))

Then click the following on the bottom of the Xojo IDE window and see what is actually happening.

why nFacets = val(txtTotalParts.text) -1 * val(txtTotalBights.text)
and not nFacets = val(txtTotalParts.text) - val(txtTotalBights.text)
???
or is it really nFacets = (val(txtTotalParts.text) -1) * val(txtTotalBights.text) ?
xojo is quite weak concerning parenthesis

Fortunately, I have an excel sheet i built this in and have something to go by as far as figures being correct on the outputs. Unfortunately, None of this worked. I’ll keep trying anyway. Thank you both for responding.

Frustrated now. I can not see where the problem is, or where I’m going wrong.
Here’s an image of what this is all about.

I am posting the whole code below.

[code]
'dim nGCD as integer
dim nParts as integer
dim nBights as integer
dim nPasses As Integer
dim nPKType As Integer
'dim nTotalParts As Integer
'dim nTotalBights As Integer
dim nHalfCycles as integer
'dim nTotalHalfCycles As Integer
dim nFacets As Integer
'dim nTotalFacets As Integer

dim A , B, nGCD as integer

nParts = val(txtParts.text) '8 in example
nBights = val(txtBights.text) '5 in example
nPasses = val(txtPasses.text)
nPKType = val(txtPKType.text)
nHalfCycles = val(txtTotalHalfCycles.text)
nFacets = val(txtTotalFacets.text)

//Math
nParts = val(txtParts.text) * val(txtPasses.text) - (val(txtPasses.text) - val(txtPKType.text) * 2)
nBights = val(txtBights.text) * val(txtPasses.text)
nHalfCycles = (val(txtTotalBights.text) * 2)
nFacets = (val(txtTotalParts.text) -1) * val(txtTotalBights.text)

//nGCD
if nBights > nParts then
A = nBights
B = nParts
else
A = nParts
B = nBights
end if

'do
'nParts =a mod b
'a = b

'b = nBights
'loop until B=0
nGCD = A
nGCD =nParts mod nBights

//now display the results by setting text fields .text property
txtTotalParts.text = str(nParts)
txtTotalBights.text = str(nBights)
txtTotalGCD.text = str(nGCD)
txtTotalHalfCycles.text = str(nHalfCycles)
txtTotalFacets.text = str(nFacets)[/code]

You are assigning different values to nHalfycles and nFacets without doing anything with the values you assigned first. Is that really correct? Or was that only for demo purposes?

Try to use a variable for every value you read instead of converting them to integers on the fly and then step through your method in the debugger, like

nParts = nParts * nPasses - (npasses - pktype * 2) and so on.

This should make it easier to debug your project. Using the values of your screenshot, you will then see that your calculation must be wrong:

5 * 2 - (2 - 1 * 2) = 10 - (2 - 2) = 10 - 0 = 10, which is what Xojo gives you.

Just two problems I’ve spotted

nParts = (val(txtParts.text) * val(txtPasses.text)) - ((val(txtPasses.text) - val(txtPKType.text)) * 2) ’ see http://www.mathsisfun.com/operation-order-bodmas.html

nFacets = (val(txtTotalParts.text) -1) * val(txtTotalBights.text) ’ you are reading out of a blank text field here

As I mentioned, put the following after every line where you assign a value and you will quickly see where you are falling over:

system.DebugLog(“nHalfCycles=” + str(nHalfCycles))

Bodmas…?
Pemdas!

[quote=315749:@Tim Parnell]Bodmas…?
Pemdas![/quote]

Hehe, we call them brackets in school, most kids (or adults, lol) don’t know what a parentheses is, great eduMacation in the uk :wink:

Brian: the code I gave you was working out the parts and bights and GCD correctly.

You have stuck some lines of code between the initisialsation of nParts and the calculation of nGCD

[quote]//Math
nParts = val(txtParts.text) * val(txtPasses.text) - (val(txtPasses.text) - val(txtPKType.text) * 2)
nBights = val(txtBights.text) * val(txtPasses.text)
nHalfCycles = (val(txtTotalBights.text) * 2)
nFacets = (val(txtTotalParts.text) -1) * val(txtTotalBights.text)[/quote]

Its here that things go wrong.

nParts was expected to be val(txtParts.text) at the point it starts to work out the nGCD

But your newly inserted code has changed it to nParts = val(txtParts.text) * val(txtPasses.text) - (val(txtPasses.text) - val(txtPKType.text) * 2)

I appreciate you are very very new to coding, but you need to use the debugger
Work through the code one line at a time and look at what happens to the contents of variable as you step through.
Dont just copy and paste a bunch of code and hope for the best.

Get the contents of the INPUT fields into integer variables before you start doing the math.
Check the integer variables hold what it said on screen, using the debugger.
Do the math, and put the results into more integer variables.

At the end, put the results of the integer variables onscreen into labels (or text boxes if you must)

[code]dim nHalfCycles as integer
dim nWraps as integer
dim nGCD as integer
dim nparts as integer
dim nbights as integer
dim A , B ,nRemainder as integer

nParts = val(txtPartsField.text) '8 in example
nBights = val(txtBightsField.text) '5 in example
nHalfCycles = nBights *2

//nGCD
if nBights > nParts then
A = nBights
B = nParts
else
A = nParts
B = nBights
end if
do
nRemainder =a mod b
a = b
b = nRemainder
loop until B=0
nGCD = A
nWraps = nBights - nGCD
nRemainder =nParts mod nBights

//NOW start to work out the values of total cycles, Facets , halfcycles etc.
//Dont change the values in nGCD,nWraps,nRemainder,nBights,nParts : these are finished and ready to use.

[/code]

Hi Jeff. This is a different program. I did recycle for lack of a better term some of the code from the algorithm generator. Here’s a image of what this one looks like.

This is what it does, This calculates the total number of parts and bights from a given base knot which the user inputs on the left hand side. Then it calculates the totals on the right hand side outputs.

A sample of what this calculator will do.

Base Knot is what you see below with no interweave.

A completed knot with interweave. The calculator calculates this complete knot including the interweave.

The GCD does not need a remainder. It just needs to calculate the total number from the right hand side.

I think IMHO that Jeff should write a book or several books on xojo. Your coding is awesome Jeff. I thank you for your help and tolerance of me a newbie.

Thank you to all for your patience with me.

Jeff you’re awesome.

Sure we do.
Its the plural of bracket. :slight_smile:

1.1parentheses A pair of round brackets ( ) used to mark off a parenthetical word or phrase.
‘the stage number is added in parentheses to the name or formula’

Not really… Brackets is the plural of bracket :wink:

Parentheses are simply brackets with osteoarthritis. :slight_smile:

Parentheses are just Mummy and Daddy brackets.