bustr
New Member
Posts: 4
|
Post by bustr on Mar 31, 2023 15:24:15 GMT
Does anyone know the solution to this? I'm in my 6th decade and not a fast learner. Struggling through such things is tiring.
Add code to ARRAYS.BAS after [quit] to display all the names entered.
wk2_challenge.html
Here is the whole macro.
'ARRAYS.BAS
'List handling with arrays
dim names$(4) 'set up our array to contain 4 items
[askForName] 'ask for a name
input "Please give me your name ?"; yourName$
if yourName$ = "" then print "No name entered." : goto [quit]
index = 0
[insertLoop]
'check to see if index points to an unused item in the array
if names$(index) = "" then names$(index) = yourName$ : goto [nameAdded]
index = index + 1 'add 1 to index
if index < 10 then [insertLoop] 'loop back until we have counted to 10
'There weren't any available slots, inform user
print "All ten name slots already used!"
goto [print]
[nameAdded] 'Notify the name add was successful
print yourName$; " has been added to the list."
goto [askForName]
[quit]
end
[print]
Print names$(i)
|
|
bustr
New Member
Posts: 4
|
Post by bustr on Mar 31, 2023 16:03:09 GMT
I found this on another forum. It does not work.
for I = 0 TO 10 print names$(I) next I
|
|
|
Post by Rod on Mar 31, 2023 16:43:06 GMT
I am on my iPhone right now but the code to print the list of items after quit that you posted is fine. The problem is that you set index to 0 every time in your main loop. Index should increase as each name is entered. So you need to set it to 0 at the start before the loop and then increment it by 1 after every valid name input. See if you can puzzle that out, that’s the fun bit😀
|
|
|
Post by tsh73 on Mar 31, 2023 18:24:40 GMT
I'm sorry to say but Rod's advice is wrong. Obviously, reading code from iPhone does not substitute for running it in BASIC :)
EDIT especially in debug mode (ladybug icon), step by step ("Step over" button, 4-th on a debugger toolbar) Do yourself a favor - try this. You will see exactly what it does , what values are in the variables etc
so
what exactly happened?
Where you put that code to? Because then I put it between [quit] and end , it does what supposed to - then you enter some names, and then enter empty one (just press ENTER without name) it prints all names that in there. (then it spills error
but 1) it already did worked 2) this error is because you DIM array to 4 items
dim names$(4) 'set up our array to contain 4 items but you ask to print 10 (actually 11, from 0 to 10)
for I = 0 TO 10 print names$(I) next I
Also I see this line
if index < 10 then [insertLoop] 'loop back until we have counted to 10 It suggests there should be maximium 10 names, not 4 ! So probably right way is
dim names$(10)
|
|
bustr
New Member
Posts: 4
|
Post by bustr on Apr 3, 2023 15:02:22 GMT
Thanks. It is still not working.
'ARRAYS.BAS
'List handling with arrays
dim names$(10) 'set up our array to contain 10 items
[askForName] 'ask for a name
input "Please give me your name ?"; yourName$
if yourName$ = "" then print "No name entered." : goto [quit]
index = 0
[insertLoop]
'check to see if index points to an unused item in the array
if names$(index) = "" then names$(index) = yourName$ : goto [nameAdded]
index = index + 1 'add 1 to index
if index < 10 then [insertLoop] 'loop back until we have counted to 10
'There weren't any available slots, inform user
print "All ten name slots already used!"
goto [quit]
[nameAdded] 'Notify the name add was successful
print yourName$; " has been added to the list."
goto [askForName]
[quit]
end
for I = 0 TO 10
print names$(I)
next I
|
|
bustr
New Member
Posts: 4
|
Post by bustr on Apr 3, 2023 15:17:49 GMT
Figured it out. Solution in bold. It doesn't print the final entry though.
'ARRAYS.BAS 'List handling with arrays dim names$(10) 'set up our array to contain 10 items [askForName] 'ask for a name input "Please give me your name ?"; yourName$ if yourName$ = "" then print "No name entered." : goto [quit] index = 0 [insertLoop] 'check to see if index points to an unused item in the array if names$(index) = "" then names$(index) = yourName$ : goto [nameAdded] index = index + 1 'add 1 to index if index < 10 then [insertLoop] 'loop back until we have counted to 10 if index = 10 then [PRINTLIST] 'There weren't any available slots, inform user print "All ten name slots already used!" goto [quit] [nameAdded] 'Notify the name add was successful print yourName$; " has been added to the list." goto [askForName] [quit] end [PRINTLIST] for I = 0 TO 10 print names$(I) next I
|
|
|
Post by tsh73 on Apr 3, 2023 15:57:32 GMT
Left is "It is still not working" Right is "it does what supposed to" In programming, single line often makes a difference.
|
|