Subscript Out of Range!

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
tavargeri
Newbie
Posts: 1
Joined: Tue May 23, 2006 4:29 pm

Subscript Out of Range!

Post by tavargeri »

i need help

Code: Select all

CLS

'main
GOSUB setup
GOSUB ask
'GOSUB wrap
END

'SETUP
setup:
LOCATE 2, 15: PRINT "My Coins Collection"
LOCATE 3, 15: PRINT "-------------------"
LOCATE 4, 15: PRINT "1 - View Database"
LOCATE 5, 15: PRINT "2 - Sort"
LOCATE 6, 15: PRINT "3 - Search"
LOCATE 7, 15: PRINT "4 - Exit Program"

h1$ = "ID  Date   Coin Name               Value     Cost"
h2$ = "--  ----   ---------               -----     ----"
p1$ = "##  ####   \                   \ $$##.##  $$##.##"

FOR x = 1 TO 10
READ id(x), date(x), name$(x), value(x), cost(x)
NEXT x
RETURN

'ASK
ask:
LOCATE 9, 15: INPUT "Enter a code, 1 through 4 === ", code
'DO WHILE code > 1 AND code < 4
   IF code = 1 THEN
   GOSUB viewd
   ELSEIF code = 2 THEN
   GOSUB sort
   ELSEIF code = 3 THEN
   'GOSUB search
   ELSEIF code = 4 THEN
   GOSUB wrapup
   ELSE
   COLOR 12
   LOCATE 10, 15: PRINT "Code out of range, program d y  i   n    g .  .  ."
   COLOR 7
   END IF
'lOOP
RETURN


'VIEWD
viewd:
CLS
PRINT h1$
PRINT h2$
FOR x = 1 TO 10
PRINT USING p1$; id(x); date(x); name$(x); value(x); cost(x)
NEXT x
RETURN

'SORT
sort:
CLS
LOCATE 1, 15: INPUT "Sort by date or value (d or v)"; dv$
IF dv$ = "d" THEN
GOSUB sortdate
ELSEIF dv$ = "v" THEN
'GOSUB sortvalue
ELSE
COLOR 12
PRINT "Invalid code.. retry"
SLEEP
COLOR 7
END IF
RETURN

'YEAR
sortdate:
  gap = 15 \ 2
  DO WHILE gap > 0
     limit = 15
     switch$ = "on"
     DO WHILE switch$ = "on"
        switch$ = "off"
        FOR date = 1 TO limit - gap
        >>>>>>>>>>>>>>>>>>>>>>>>>>>>IF b(date) > b(date + gap) THEN
           SWAP b(id), b(id + gap)
           SWAP b(date), b(date + gap)
           'SWAP b(name$), b(name$ + gap)
           SWAP b(value), b(value + gap)
           SWAP b(cost), b(cost + gap)
           stich$ = "on"
           last = year
        END IF
     NEXT date
     limit = last
  LOOP
  gap = gap \ 2
LOOP
PRINT h1$
PRINT h2$
FOR x = 1 TO 10
PRINT USING p1$; id(x); date(x); name$(x); value(x); cost(x)
NEXT x

'WRAP
wrapup:
CLS
LOCATE 12, 31: PRINT "END PROGRAM"
END



DATA 1, 2002, Nixon Dime, 1.30, 1.15
DATA 5, 1954, Carter Quarter, 1.10, 1.05
DATA 4, 1875, Tavargeri Dollar, 9.90, 8.00
DATA 7, 1999, Bush Nickel, 1.00, 0.05
DATA 10, 2006, Ford Penny, 1.10, 1.02
DATA 13, 2001, Eagle Quarter, 5.00, 2.00
DATA 15, 1991, Liberty Gold Dollar, 6.20, 1.33
DATA 80, 1987, Lincoln Penny, 1.06, 1.04
DATA 9, 1945, New Jersey Quarter, 1.20, 1.15
DATA 14, 1866, Indian Rupees, 1.50, 1.00
DATA 21, 2006, European Euro, 2.00, 1.30
DATA 22, 2006, French Franc, 1.50, 1.00
DATA 63, 2007, New Jersey Penny, 8.54, 5.00
DATA 99, 1987, California Gold Dollar, 1.32, 0.44
DATA 16, 400, Homosapien Rock, 9.36, 5.12
the line with all the arrows is where i get the error. can anyone help??

IF b(date) > b(date + gap) THEN
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

tavargeri wrote:i need help

Code: Select all

.....
FOR x = 1 TO 10
READ id(x), date(x), name$(x), value(x), cost(x)
NEXT x
......
sortdate:
  gap = 15 \ 2
  DO WHILE gap > 0
     limit = 15
     switch$ = "on"
     DO WHILE switch$ = "on"
        switch$ = "off"
        FOR date = 1 TO limit - gap
        >>>>>>>>>>>>>>>>>>>>>>>>>>>>IF b(date) > b(date + gap) THEN
           SWAP b(id), b(id + gap)
           SWAP b(date), b(date + gap)
           'SWAP b(name$), b(name$ + gap)
           SWAP b(value), b(value + gap)
           SWAP b(cost), b(cost + gap)
           stich$ = "on"
           last = year
        END IF
     NEXT date
Tavargeri, my friend, you have lots of problems in this program.

1) First of all you should always DIM (dimension) all your arrays. Basic has a feature whereby you don't have to dimension an array if it has 10 or less elements. You should ignore this pitfall, and always DIM your arrays for definition and clarity.

2) Also, I don't know what version of QB, Quickbasic or PDS you're using, but normally variable names like "date" and "name$" are reserved. You should avoid using them.

2) Ok, you didn't DIM arrays id, date, name$, value, and cost, so we'll assume that they never will have more than 10 elements by default, plus the fact that you only READ the DATA statements 10 times. That's funny because you have 15 DATA statements. Don't you want to use the last 5?

3) Now to the "subscript out of range" error. What is the b() array? Again assuming that it can only have 10 elements by default, there is no such thing as b(date), because "date" is an array not a variable. I don't know why your Basic allowed this. Also, for the same reason, you can't do "for date = 1 to limit - gap".

Anyway the statement "for date = 1 to limit - gap" will produce values of the variable "date" greater than 10, so that's where the error is issued.

4) By the way, the subroutine "sortdate" has no RETURN at the end.

5) Once you have all you arrays and variables properly defined, you will have to re-write the entire "sortdate" subroutine. I also can't figure out what sort algorithm you're trying to implement. For 15 elements, pick the simplest version of a Bubble sort that you can find.

As a matter of fact, since your data is splashed across 5 arrays, it might be simpler not to sort the data at all. Make two copies or sets of the DATA statements, one copy hand-sorted by date and the other sorted by value. When you read them in, put the sorted sets into 2 sets of separate arrays, like id1(), date1(), etc. and id2(), date2(), etc. You have plenty of room with only 15 elements in each array. Then when you want to print out the stuff sorted by date, use arrays id1, date1, etc. This way you'll never have sorting problems.

PS: I spent a considerable amount of time analysing your program, so I would appreciate some feedback.

*****
Post Reply