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

Subscript out Of Range??

Post by JasonQuinn1992 » Tue Oct 02, 2012 10:32 pm

What does it mean if you get an error stating "Subscript out Of Range"?

How would I fix it on this code:



' Initialize variables
Pieces = -1
Pay# = 0
TotPieces = 0
TotPay = 0
T1$ = "PIECEWORK WEEKLY REPORT"
H1$ = "NAME" + SPC$(16) + "Pieces" + SPC$(6) + "Pay"
D1$ = ""
TL$ = ""
Name$ = ""

'Get first worker name
INPUT "PLEASE ENTER YOUR NAME OR TYPE 'END' TO QUIT"; Name$

DO
'Get worker pieces
INPUT "PLEASE ENTER THE NUMBER OF PIECES OR TYPE 'END' TO QUIT"; Pieces

'Calculate rate
IF Pieces >= 600 THEN
Rate = 65
ELSEIF Pieces >= 400 AND Pieces <599>= 200 AND Pieces <399>= 1 AND Pieces <= 199 THEN
Rate = 5
ELSE Rate = 0
END IF

'Calculate pay
IF Rate > 0 THEN Pay = Pieces * Rate ELSE Pay = 0

'Running Totals
TotPieces = TotPieces + Pieces
TotPay = TotPay + Pay
PayF$ = " "
RSET PayF$ = STR$(Pay)
D1$ = D1$ + Name$ + SPC$(20 - LEN(Name$)) + STR$(Pieces) + SPC$(10 - LEN(STR$(Pieces))) + PayF$ + CHR$(13)

'Get next worker or END to quit
INPUT "PLEASE ENTER YOUR NAME"; Name$

LOOP UNTIL UCASE$(Name$) = "END"

'Clear screen
CLS

' Write headings
PRINT TAB(10); T1$
PRINT H1$

'Print report detail
PRINT D1$
ToTPayF$ = " "
RSET ToTPayF$ = STR$(TotPay)
PRINT " TOTALS" + SPC$(12) + STR$(TotPieces) + SPC$(10 - LEN(STR$(TotPieces))) + ToTPayF$
END
'**********************************************************************


its happening at the "H1$ = "NAME" + SPC$(16) + "Pieces" + SPC$(6) + "Pay" line

OPRESION
Veteran
Posts: 61
Joined: Tue Jan 16, 2007 4:15 am
Location: Mexico

Post by OPRESION » Wed Oct 03, 2012 10:22 am

'SUBSCRIPTION OUT OF RANGE' IT'S AN ERROR WHEN YOU OPEN A MATRIX AND
YOU TRY TO USE THAT MATRIX BEYOND THE LIMITS. FOR EXAMPLE IF YOU OPENED...

DIM AAA(100) AS INTEGER

AND AFTER THAT YOU TRY TO DO THIS...

AAA(101) = 22222

THEN YOU WILL HAVE THE ERROR 'SUBSCRIPTION OUT OF RANGE'

THE FUNCTION SPC() IT DOESN'T NEED THE STRINGS SIGN: '$' BECAUSE
THE COMPILER WILL THINK THAT IT IS A MATRIX. YOU LINE MUST BE:

Code: Select all

 H1$ = "NAME" + SPC(16) + "Pieces" + SPC(6) + "Pay" 
MY PAGE: http://Qbasic.phatcode.net" target="_blank
(I ONLY USE WINDOWS 98SE YET, BELIEVE IT OR NOT)

JasonQuinn1992
Coder
Posts: 42
Joined: Mon Sep 03, 2012 4:32 pm

Post by JasonQuinn1992 » Wed Oct 03, 2012 11:07 am

Thank you for the reply, I tried getting rid of the "$" sign but then I get an error on SPC(16) stating Expected expression, so my program wont run.. how should I fix this?

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

Post by burger2227 » Wed Oct 03, 2012 1:46 pm

SPC$ denotes a STRING ARRAY. SPC prints spaces!

Code: Select all

' Initialize variables
Pieces = -1
Pay# = 0
TotPieces = 0
TotPay = 0
T1$ = "PIECEWORK WEEKLY REPORT"
H1$ = "NAME" + SPC(16) + "Pieces" + SPC$(6) + "Pay"
D1$ = ""
TL$ = ""
Name$ = ""

'Get first worker name
INPUT "PLEASE ENTER YOUR NAME OR TYPE 'END' TO QUIT"; Name$

DO
  'Get worker pieces
  INPUT "PLEASE ENTER THE NUMBER OF PIECES OR TYPE 'END' TO QUIT"; Pieces

  'Calculate rate
  IF Pieces >= 600 THEN
    Rate = 65
  ELSEIF Pieces >= 400 AND Pieces <599>= 200 AND Pieces <399>= 1 AND Pieces <= 199 THEN
    Rate = 5
  ELSE Rate = 0
  END IF

  'Calculate pay
  IF Rate > 0 THEN Pay = Pieces * Rate ELSE Pay = 0

  'Running Totals
  TotPieces = TotPieces + Pieces
  TotPay = TotPay + Pay
  PayF$ = " "
  RSET PayF$ = STR$(Pay)
  D1$ = D1$ + Name$ + SPC(20 - LEN(Name$)) + STR$(Pieces) + SPC$(10 - LEN(STR$(Pieces))) + PayF$ + CHR$(13)

  'Get next worker or END to quit
  INPUT "PLEASE ENTER YOUR NAME"; Name$

LOOP UNTIL UCASE$(Name$) = "END"

'Clear screen
CLS

' Write headings
PRINT TAB(10); T1$
PRINT H1$

'Print report detail
PRINT D1$
ToTPayF$ = " "
RSET ToTPayF$ = STR$(TotPay)
PRINT " TOTALS" + SPC(12) + STR$(TotPieces) + SPC$(10 - LEN(STR$(TotPieces))) + ToTPayF$
END
'**********************************************************************
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 » Wed Oct 03, 2012 2:21 pm

Ok, yeah.. I need spaces.... but when i try to run the program i still get an expected expression error and when i change it to SPC$ i get a subscript out of range error

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

Post by burger2227 » Wed Oct 03, 2012 5:33 pm

Yeah, QB requires SPACE$ in string definitions. SPC is suppposed to be used to move the invisible PRINT cursor on the screen. I also took off the END exit option because the INPUT requires a number, not a string! If you type END instead of a number it will REDO FROM START.

You could use a string variable for the number INPUT and use VAL to convert it to a number if it is not "END". VAL("END") = 0 so you could also exit if the number value is 0...

Code: Select all

' Initialize variables
Pieces = -1
Pay# = 0
TotPieces = 0
TotPay = 0
T1$ = "PIECEWORK WEEKLY REPORT"
H1$ = "NAME" + SPACE$(16) + "Pieces" + SPC$(6) + "Pay"
D1$ = ""
TL$ = ""
Name$ = ""

'Get first worker name
INPUT "PLEASE ENTER YOUR NAME OR TYPE 'END' TO QUIT: "; Name$

DO
  'Get worker pieces
  INPUT "PLEASE ENTER THE NUMBER OF PIECES: "; Pieces

  'Calculate rate
  IF Pieces >= 600 THEN
    Rate = 65
  ELSEIF Pieces >= 400 AND Pieces <599>= 200 AND Pieces <399>= 1 AND Pieces <= 199 THEN
    Rate = 5
  ELSE Rate = 0
  END IF

  'Calculate pay
  IF Rate > 0 THEN Pay = Pieces * Rate ELSE Pay = 0

  'Running Totals
  TotPieces = TotPieces + Pieces
  TotPay = TotPay + Pay
  PayF$ = " "
  RSET PayF$ = STR$(Pay)
  D1$ = D1$ + Name$ + SPACE$(20 - LEN(Name$)) + STR$(Pieces) + SPC$(10 - LEN(STR$(Pieces))) + PayF$ + CHR$(13)

  'Get next worker or END to quit
  INPUT "PLEASE ENTER YOUR NAME"; Name$

LOOP UNTIL UCASE$(Name$) = "END"

'Clear screen
CLS

' Write headings
PRINT TAB(10); T1$
PRINT H1$

'Print report detail
PRINT D1$
ToTPayF$ = " "
RSET ToTPayF$ = STR$(TotPay)
PRINT " TOTALS" + SPACE$(12) + STR$(TotPieces) + SPC$(10 - LEN(STR$(TotPieces))) + ToTPayF$
END
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 » Wed Oct 03, 2012 9:11 pm

Thank you so much that was exactly what I needed.. the program works perfectly :)

Post Reply