Qbasicnews.com

Full Version: Array help please
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to create an array to store string values for 12 days. I then want to ask the user how many days they would like to purchase items for and display the items for each of the days up to the number inputed.

here is what i have so far. any help would be greatly appreciated.

Code:
DIM n AS INTEGER
DIM day AS INTEGER

DIM items$(12)
items$(1) = "Partridge in a pear tree"
items$(2) = "Turtledove"
items$(3) = "French hen"
items$(4) = "Calling bird"
items$(5) = "Gold ring"
items$(6) = "Geese-a-laying"
items$(7) = "Swan-a-swimming"
items$(8) = "Maid-a-milking"
items$(9) = "Lady dancing"
items$(10) = "Lord-a-leaping"
items$(11) = "Piper piping"
items$(12) = "Drummer drumming"

CLS
1 PRINT "Enter amount of item to buy": INPUT day
        IF day < 1 THEN
    PRINT "Surely you can afford 1 day!"
        SLEEP 7
        GOTO 1
        ELSEIF day > 12 THEN
        PRINT "Don't be greedy!"
        SLEEP 7
        GOTO 1
        END IF

FOR n = 1 TO day
        PRINT items$
        n = n + 1
NEXT

END
As far as I can see, you've done a good job. Except for the last:
Code:
FOR n = 1 TO day
        PRINT items$
        n = n + 1
NEXT
which should be:
Code:
FOR n = 1 TO day
        PRINT items$(n)
NEXT

Anonymous

To clarify:

FOR... NEXT automatically counts up for every loop iteration. Theres also a command called STEP for it, which works like this


Code:
FOR p = 1 TO 7 STEP 2
  PRINT p

NEXT p

This prints 1, 3, 5, 7. So if you don't specify a STEP, it is assumed to be 1 =)