Calculating pounds, shillings, and pence - where am I going wrong?

I’m trying to write a code that displays for old pre-decimal British currency, just a little proof of concept right now. I think I’ve coded it correctly - all the other options except for the first two work. For those two, Dashingdon freezes without an error message when I try running it. Where am I going wrong? I want to be able to calculate into the tens and potentially hundreds of pounds, so the game freezing on larger amounts is a little worrying.

https://dashingdon.com/play/armadillidium/pounds-shillings-pence/mygame/
https://dashingdon.com/play/armadillidium/pounds-shillings-pence/mygame/scenes/startup.txt

(Conversion: 1 pound = 20 shillings, and 1 shilling = 12 pence. 1 pound = 240 pence. I could subdivide the penny further into farthings, but that’s probably unnecessary because it’s chump change for my character.)

I tried coding it separately as pounds, shilling, and pence in another document, but found that’s more trouble to code than it’s worth when requiring checking if the character can afford something.

Also I’m getting this error after the finish, but that’s not very important right now. Nice if could figure out why, though: “invalid return; we’ve already returned from the last gosub”, both for lines 155 (with money set to 0) and 159 (with money not 0).

Thanks

The problem is this block of code:

*label moneydisplay_1
*set dd 0
*set ss 0
*set ll 0
*set dd "${money}"
*if dd >= 240
	*set dd -240
	*set ll +1
	*if dd >= 240
		*goto moneydisplay_1
	*else
		*goto moneydisplay_2

The crucial issue here is that dd gets set to the value of money every time you enter the loop. The first two options in the initial choice set money to 1200 and 900 respectively; since dd therefore exceeds 240, it jumps into the first *if statement, 240 is subtracted from dd, and then (because dd still exceeds 240) it *gotos back up to moneydisplay_1…where dd gets set to the value of money again, i.e. 1200 or 900, and this just keeps repeating because the value of money never changes.

Basically, you’re trapping yourself in an infinite loop.

3 Likes

Thank you! That worked. Moved the first bunch of sets to a separate label.

*label moneydisplay_0
*set dd 0
*set ss 0
*set ll 0
*set dd "${money}"
*goto moneydisplay_1

*label moneydisplay_1
*if dd >= 240
	*set dd -240
	*set ll +1
	*if dd >= 240
		*goto moneydisplay_1
	*else
		*goto moneydisplay_2

Any clue with the invalid return?

That one I’m not sure about, sorry. Generally what that error means is that you’ve hit a *return command without first jumping in via a *gosub (meaning the code doesn’t have anywhere to “return” to), but I’m not really sure how that could happen the way that it does. Seems like the code must be stumbling into the “LOOPS” section somehow, but again, not sure why simply hitting “Play again” would cause it to do that.

Alright, let’s not worry about it now then, it doesn’t seem like anything game-breaking. Cross the bridge when I get there. Thanks!

I remember seeing a question like this one on LeetCode, you can check it out, might give you some inspirations.

This topic was automatically closed 24 hours after the last reply. If you want to reopen your WiP, contact the moderators.