Subscript out Of Range error (UPDATED CODE)

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
JasonQuinn1992
Coder
Posts: 42
Joined: Mon Sep 03, 2012 4:32 pm

Subscript out Of Range error (UPDATED CODE)

Post by JasonQuinn1992 »

Why would I be getting an Subscript out of range error on this line?

IF NumPersons(N) <= 2 THEN

My Code:


DIM IDNum(14), AnIncome(14), NumPersons(14)


GOSUB InitializeVariables
GOSUB LoadTables
GOSUB CalcPoverty
GOSUB CalcAvg


InitializeVariables:
LET TotIncome = TotIncome + AnIncome(N) 'Calc total
LET TotBelowPov = TotBelowPov + BelowPov(N) 'Calc total below pov
LET AveIncome = TotIncome / 14 'Calc average income
LET PercentBelowPov = TotBelowPov / 14 * 100 'Calc % below pov
LET AboveAve(N) = 48
LET PageCt = 0
LET IDNum = 0
LET NumPersons = 0
PRINT T1$ = " Income Survey PAGE ##"
LET H1$ = ""
PRINT H2$ = " ID Number ANNUAL INCOME NUMBER OF PERSONS "
PRINT D1$ = " #### $#,###.## #"
PRINT ST$ = " / \ SUBTOTAL #"
PRINT TL$ = " TOTAL #"
LET MaxLines = 30
RETURN

'******************************
' Read numbers into the array
' *****************************


LoadTables:
FOR N = 1 TO 14
READ IDNum(N), AnIncome(N), NumPersons(N)
NEXT N

DATA 2497, 12,500, 2
DATA 3323, 13,00, 5
DATA 4521, 18,210, 4
DATA 6789, 8,000, 2
DATA 5476, 6,000, 1
DATA 4423, 16,400, 3
DATA 6587, 25,000, 4
DATA 3221, 10,500, 4
DATA 5555, 15,000, 2
DATA 0085, 19,700, 3
DATA 3097, 20,000, 8
DATA 4480, 23,400, 5
DATA 0265, 19,700, 2
DATA 8901, 13,000, 3

CalcPoverty:
IF NumPersons(N) <2> 2 THEN
PovertyLevel(N) = 8000 + (2000 * (NumPersons(N) - 2))
END IF
IF AnIncome(N) <PovertyLevel> AveIncome THEN
AboveAve(N) = 1
ELSE
AboveAve(N) = 0
END IF
NEXT N
RETURN
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

That is not what appears in your code.

Code: Select all

DIM IDNum(14), AnIncome(14), NumPersons(14) 


GOSUB InitializeVariables 
GOSUB LoadTables 
GOSUB CalcPoverty 
GOSUB CalcAvg 


InitializeVariables: 
LET TotIncome = TotIncome + AnIncome(N) 'Calc total 
LET TotBelowPov = TotBelowPov + BelowPov(N) 'Calc total below pov 
LET AveIncome = TotIncome / 14 'Calc average income 
LET PercentBelowPov = TotBelowPov / 14 * 100 'Calc % below pov 
LET AboveAve(N) = 48 
LET PageCt = 0 
LET IDNum = 0 
LET NumPersons = 0 
PRINT T1$ = " Income Survey PAGE ##" 
LET H1$ = "" 
PRINT H2$ = " ID Number ANNUAL INCOME NUMBER OF PERSONS " 
PRINT D1$ = " #### $#,###.## #" 
PRINT ST$ = " / \ SUBTOTAL #" 
PRINT TL$ = " TOTAL #" 
LET MaxLines = 30 
RETURN 

'****************************** 
' Read numbers into the array 
' ***************************** 


LoadTables: 
FOR N = 1 TO 14 
READ IDNum(N), AnIncome(N), NumPersons(N) 
NEXT N 
RETURN '<<<<<< ???

DATA 2497, 12,500, 2 
DATA 3323, 13,00, 5 
DATA 4521, 18,210, 4 
DATA 6789, 8,000, 2 
DATA 5476, 6,000, 1 
DATA 4423, 16,400, 3 
DATA 6587, 25,000, 4 
DATA 3221, 10,500, 4 
DATA 5555, 15,000, 2 
DATA 0085, 19,700, 3 
DATA 3097, 20,000, 8 
DATA 4480, 23,400, 5 
DATA 0265, 19,700, 2 
DATA 8901, 13,000, 3 

CalcPoverty: 
IF NumPersons(N) <= 2 THEN 
PovertyLevel(N) = 8000 + (2000 * (NumPersons(N) - 2)) 
END IF 
IF AnIncome(N) < PovertyLevel THEN 
AboveAve(N) = 1 '<<<<<<<<<<< DIM?
ELSE 
AboveAve(N) = 0 
END IF 
NEXT N  ' <<<<<<<<< NO FOR?
RETURN
Why is your code always incomplete? Check your work.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
JasonQuinn1992
Coder
Posts: 42
Joined: Mon Sep 03, 2012 4:32 pm

Post by JasonQuinn1992 »

The code is complete the NEXT is for the next number
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Look at the bottom of the code! <<<<<<NO FOR

NEXT N should not be there.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
JasonQuinn1992
Coder
Posts: 42
Joined: Mon Sep 03, 2012 4:32 pm

Post by JasonQuinn1992 »

I was able to fix it:

part of it:


CalcPoverty:
IF NumPersons(N) <2> 2 THEN
PovertyLevel(N) = 8000 + (2000 * (NumPersons(N) - 2))
END IF
IF AnIncome(N) < PovertyLevel(N) THEN
LET TotBelowPov = TotBelowPov + 1
END IF
RETURN

PrintDetail:
PRINT H2$
FOR N = 1 TO 14
PRINT USING D1$; IDNum(N); AnIncome(N); NumPersons(N) 'Print details
NEXT N
RETURN

PrintSubtotals:
LET AvgIncome = TotIncome / 14 'Calc average income
LET PercentBelowPov = TotBelowPov / 14 * 100 'Calc % below pov
RETURN

PrintTotals:
PRINT USING "$##,###.## \ \"; AvgIncome; "is the average income"
GOSUB PrintAboveAverage
PRINT USING "##.#% \ \"; PercentBelowPov; "of households are below the poverty line"
RETURN

PrintAboveAverage:
PRINT H2$
FOR N = 1 TO 14
IF AnIncome(N) > AvgIncome THEN
PRINT USING D1$; IDNum(N); AnIncome(N); NumPersons(N)
END IF
NEXT N
RETURN
Post Reply