Subscript out Of Range error

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

Post by JasonQuinn1992 » Thu Nov 01, 2012 3:58 pm

Why am I getting an subscript out of range error on this line?

AboveAve(N) = 0

My code:


DIM AnnIncome(40) AS INTEGER

' Variables used:
' T1$ H1$, H2$, D1$ Print images
' ST$, TL$ Print images
' PrevTotal Previous Total
' LineCt Line number
' PageCt Page number
' TotHours Total
' IDNum ID Number
' AnnIncome$ Annual Income
' NumPersons Number of Persons


' ***********************************************************************
' * PROGRAM MAINLINE *
' ***********************************************************************

CLS
GOSUB InitializeVariables
GOSUB PrintHeadings
GOSUB ProcessDetail
GOSUB CalculatePovertyLevel
GOSUB CalculateAboveAve
END

' ***********************************************************************
' * INITIALIZE VARIABLES *
' ***********************************************************************

InitializeVariables:
LET PageCt = 0
LET Rate = 0
LET Hours = 0
LET T1$ = " Income Survey PAGE ##"
LET H1$ = ""
LET H2$ = " ID Number ANNUAL INCOME NUMBER OF PERSONS "
LET D1$ = " #### $#,###.## #"
LET ST$ = " / \ SUBTOTAL #"
LET TL$ = " TOTAL #"
LET MaxLines = 30
RETURN

' ***************************************************************************
' * LPRINT HEADINGS *
' ***************************************************************************

PrintHeadings:
LET PageCt = PageCt + 1 'Add to page counter
PRINT CHR$(12); 'Advance to tope of page
PRINT USING T1$; PageCt 'Print title line
PRINT
PRINT H1$ 'Print column headings, line 1
PRINT H2$ 'Print column headings, line 2
PRINT
LET LineCt = 0 'Reset line counter
RETURN

' **********************************************************************************
' PROCESS DATA
' ***********************************************************************************
ProcessDetail:

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


' *************************************************************************
' * READ DATA *
' *************************************************************************

ReadData:
READ IDNum, AnnIncome, NumPersons
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
RETURN


CalculatePovertyLevel:
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







Also, how would I print this program as a 3-part report

User avatar
burger2227
Veteran
Posts: 2435
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 » Fri Nov 02, 2012 12:35 am

DIM the array to use it. It has to be sized if it holds 10 or more values.
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 » Fri Nov 02, 2012 12:01 pm

Just "DIM"ed it, but am still getting the eror

User avatar
burger2227
Veteran
Posts: 2435
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 » Fri Nov 02, 2012 2:54 pm

Your code is incomplete. What is NEXT N doing?
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 » Fri Nov 02, 2012 5:38 pm

Counting the next number..... I have to print a 3 part report about income


I added a print statement after the calculations

User avatar
burger2227
Veteran
Posts: 2435
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 » Fri Nov 02, 2012 9:25 pm

NEXT requires a FOR statement before it.
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 » Fri Nov 02, 2012 10:51 pm

I believe I corrected it:

My full code:

DIM AnIncome(100)



' Variables used:
' T1$ H1$, H2$, D1$ Print images
' ST$, TL$ Print images
' PrevTotal Previous Total
' LineCt Line number
' PageCt Page number
' TotHours Total
' IDNum ID Number
' AnIncome$ Annual Income
' NumPersons Number of Persons


' ***********************************************************************
' * PROGRAM MAINLINE *
' ***********************************************************************

CLS
GOSUB InitializeVariables
GOSUB PrintHeadings
GOSUB ProcessDetail
GOSUB CalculatePovertyLevel
GOSUB CalculateAboveAve
END

' ***********************************************************************
' * INITIALIZE VARIABLES *
' ***********************************************************************

InitializeVariables:
LET PageCt = 0
LET Rate = 0
LET Hours = 0
LET T1$ = " Income Survey PAGE ##"
LET H1$ = ""
LET H2$ = " ID Number ANNUAL INCOME NUMBER OF PERSONS "
LET D1$ = " #### $#,###.## #"
LET ST$ = " / \ SUBTOTAL #"
LET TL$ = " TOTAL #"
LET MaxLines = 30
RETURN

' ***************************************************************************
' * LPRINT HEADINGS *
' ***************************************************************************

PrintHeadings:
LET PageCt = PageCt + 1 'Add to page counter
PRINT CHR$(12); 'Advance to tope of page
PRINT USING T1$; PageCt 'Print title line
PRINT
PRINT H1$ 'Print column headings, line 1
PRINT H2$ 'Print column headings, line 2
PRINT
LET LineCt = 0 'Reset line counter
RETURN

' **********************************************************************************
' PROCESS DATA
' ***********************************************************************************
ProcessDetail:

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


' *************************************************************************
' * READ DATA *
' *************************************************************************

ReadData:
READ IDNum, AnIncome, NumPersons
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
RETURN


CalculatePovertyLevel:
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

PrintDetail:
PRINT USING D1$; IDNum; AnIncome; NumPersons 'Print details
RETURN

Post Reply