Sub Script 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
TheWicker
Newbie
Posts: 5
Joined: Thu Oct 05, 2006 11:56 pm
Location: Tucson AZ
Contact:

Sub Script out of range

Post by TheWicker » Fri Oct 06, 2006 12:00 am

I am getting a Sub Script out of range error on this, any one know how to fix it?
dim stu as string
dim class as string
dim gpa as integer
dim freshSTU(30) as string
dim freshGPA(30) as integer
dim sophSTU(30) as string
dim sophGPA(30) as integer
dim juniSTU(30) as string
dim juniGPA(30) as integer
dim seniSTU(30) as string
dim seniGPA(30) as integer
dim x as integer

open "students.txt" for input as #1
x = 0
while not EOF(1)
input #1, stu, class, gpa

if class = "Freshman" then
freshSTU(x) = stu
freshGPA(x) = gpa
x = x + 1
elseif class = "Sophomore" then
sophSTU(x) = stu
sophGPA(x) = gpa
x = x + 1
elseif class = "Junior" then
juniSTU(x) = stu
juniGPA(x) = gpa
x = x + 1
elseif class = "Senior" then
seniSTU(x) = stu
seniGPA(x) = gpa
x = x + 1
endif
wend

while x < 30
print freshSTU(x), freshGPA(x)
x = x + 1
wend

bungytheworm
Veteran
Posts: 288
Joined: Sat Feb 18, 2006 4:02 pm

Post by bungytheworm » Fri Oct 06, 2006 8:03 am

Hi TheWicker.
Could you post your students.txt too?
As far as i see, there should be no problems on that code of your.
Fix me fellas if im wrong. Cant acces to qbasic atm.

User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} » Fri Oct 06, 2006 1:58 pm

Please post your student.txt and please tell me the line number. I see nothing wrong with your code right now.

Subscript out of Range is a terrible error; it can mean way to many things. It just doesn't make sense. Stupid microshit.
Image

TheWicker
Newbie
Posts: 5
Joined: Thu Oct 05, 2006 11:56 pm
Location: Tucson AZ
Contact:

Post by TheWicker » Fri Oct 06, 2006 2:42 pm

The Students.txt =

STU0758,Freshman,2.81
STU0454,Senior,2.03
STU0584,Senior,2.56
STU0429,Freshman,3.71
STU0470,Freshman,2.58
STU0702,Junior,3.46
STU0645,Junior,3.18
STU0558,Freshman,3.72
STU0531,Junior,1.22
STU0668,Freshman,1.23
STU0533,Senior,1.12
STU0800,Senior,3.33
STU0349,Junior,1.91
STU0837,Sophomore,3.07
STU0403,Junior,2.84
STU0056,Sophomore,2.15
STU0541,Junior,3.75
STU0765,Freshman,2.72
STU0987,Senior,3.55
STU0820,Sophomore,1.23
STU0867,Freshman,3.69
STU0166,Sophomore,2.18
STU0995,Senior,2.72
STU0677,Sophomore,1.50
STU0236,Freshman,2.08
STU0145,Senior,3.81
STU0783,Sophomore,3.78
STU0497,Sophomore,2.13
STU0405,Senior,3.93
STU0726,Senior,2.89
STU0070,Freshman,2.22
STU0804,Junior,3.75
STU0241,Freshman,3.47
STU0916,Freshman,1.42
STU0093,Freshman,3.14
STU0576,Sophomore,1.19
STU0844,Sophomore,3.09
STU0378,Freshman,2.72
STU0125,Sophomore,2.93
STU0065,Junior,3.29
STU0600,Freshman,3.48
STU0043,Junior,1.89
STU0592,Sophomore,1.51
STU0251,Junior,1.49
STU0543,Junior,3.21
STU0302,Freshman,1.59
STU0803,Senior,2.66
STU0982,Sophomore,2.95
STU0046,Senior,2.18
STU0644,Junior,1.52
STU0419,Senior,2.21
STU0570,Junior,3.41
STU0126,Freshman,1.13
STU0119,Junior,2.11
STU0101,Freshman,2.36
STU0462,Senior,3.29
STU0893,Freshman,1.72
STU0738,Senior,1.19
STU0724,Sophomore,3.23
STU0228,Sophomore,1.74
STU0430,Sophomore,1.74
STU0996,Junior,1.06
STU0219,Junior,2.41
STU0476,Sophomore,2.50
STU0104,Junior,2.76
STU0740,Sophomore,3.90
STU0569,Senior,2.41
STU0110,Junior,1.83
STU0620,Senior,2.54
STU0779,Sophomore,3.87
STU0556,Sophomore,2.18
STU0152,Freshman,3.27


and I am getting the error on all of the case lines that assign stu or gpa to the arrays

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Fri Oct 06, 2006 4:22 pm

Correct me if I'm wrong, but with the INPUT #N statement, don't you need quotes around the strings? If so, then put quotes around all of the "STUXXXXX" and the "FRE/SOP/JUN/SEN". That will probably fix it.

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo » Fri Oct 06, 2006 6:15 pm

I think the problem is the following:

Code: Select all

while x < 30 
print freshSTU(x), freshGPA(x) 
x = x + 1 
wend
Before the while, you need to set x=0,
otherwise the while will start off with x equal to what it was before.

*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.

bungytheworm
Veteran
Posts: 288
Joined: Sat Feb 18, 2006 4:02 pm

Post by bungytheworm » Fri Oct 06, 2006 6:25 pm

STU0758,Freshman,2.81
dim stu as string
dim class as string
dim gpa as integer
Is it my eyes or what but i do see String, String, Double on that .txt file?

TheWicker
Newbie
Posts: 5
Joined: Thu Oct 05, 2006 11:56 pm
Location: Tucson AZ
Contact:

Post by TheWicker » Fri Oct 06, 2006 10:18 pm

Thanks for all the feed back guys, but none of the suggestions so far have fixed it. :(

Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re » Sat Oct 07, 2006 9:13 am

You dimension your arrays to hold 31 entries (0 to 30), but you read 72.
Subscript out of range.

Change your array dims to 100 or something. And hey, pay attention in class.
I have left this dump.

TheWicker
Newbie
Posts: 5
Joined: Thu Oct 05, 2006 11:56 pm
Location: Tucson AZ
Contact:

Post by TheWicker » Sat Oct 07, 2006 11:57 am

That fixed the subscript error, but the print statement only prints some random entries out of the array. The reason i used 30 as the dimmension, was because my prof told us that was as big as they needed to be. :|

here is the updated code
DIM stu AS STRING
DIM class AS STRING
DIM GPA AS DOUBLE
DIM freshSTU(100) AS STRING
DIM freshGPA(100) AS DOUBLE
DIM sophSTU(100) AS STRING
DIM sophGPA(100) AS DOUBLE
DIM juniSTU(100) AS STRING
DIM juniGPA(100) AS DOUBLE
DIM seniSTU(100) AS STRING
DIM seniGPA(100) AS DOUBLE
DIM x AS INTEGER
OPEN "students.txt" FOR INPUT AS #1
CLS
x = 0
WHILE NOT EOF(1)
INPUT #1, stu, class, GPA
SELECT CASE class
CASE "Freshman"
freshSTU(x) = stu
freshGPA(x) = GPA
x = x + 1

CASE "Sophomore"
sophSTU(x) = stu
sophGPA(x) = GPA
x = x + 1
CASE "Junior"
juniSTU(x) = stu
juniGPA(x) = GPA
x = x + 1
CASE "Senior"
seniSTU(x) = stu
seniGPA(x) = GPA
x = x + 1
END SELECT
WEND

x = 0
FOR x = 0 TO 100
PRINT x, sophSTU(x), sophGPA(x)
x = x + 1
SLEEP
NEXT x

bungytheworm
Veteran
Posts: 288
Joined: Sat Feb 18, 2006 4:02 pm

Post by bungytheworm » Sat Oct 07, 2006 2:17 pm

Code: Select all

x = 0
FOR x = 0 TO 100
PRINT x, sophSTU(x), sophGPA(x)
x = x + 1
SLEEP
NEXT x 
Why you grow x + 1 even you are using FOR NEXT loop that does increase value of x every round?

When you are reading from students.txt, you increase value of x every round.
Is that really your purpose? Since x gets + 1 every round, two arrays() gets empty cells.

example: lets think x has value 5
CASE "Sophomore"
sophSTU(x) = stu
sophGPA(x) = GPA
x = x + 1 ' now x is 6

rest of arrays(5) dont get anything. If you wana create seperate arrays, you need to use different counter (this case only counter is x) for each array.

I edited your code a bit. I hope that explains it better than my bad english.
With this one, you can decrease also your array entries. I leaved them to 100. Dont wana give you finished code.

Let us know if this not helping.

Code: Select all

CLS
DIM stu AS STRING
DIM class AS STRING
DIM GPA AS DOUBLE

DIM freshSTU(100) AS STRING
DIM freshGPA(100) AS DOUBLE
DIM freshX AS Integer
	freshX = 0

DIM sophSTU(100) AS STRING
DIM sophGPA(100) AS DOUBLE
DIM sophX AS Integer
	sophX = 0

DIM juniSTU(100) AS STRING
DIM juniGPA(100) AS DOUBLE
DIM juniX AS Integer
	juniX = 0

DIM seniSTU(100) AS STRING
DIM seniGPA(100) AS DOUBLE
DIM seniX AS Integer
	seniX = 0

DIM x AS Integer

OPEN "students.txt" FOR INPUT AS #1
	WHILE NOT EOF(1)
	INPUT #1, stu, class, GPA

		SELECT CASE class

		CASE "Freshman"
			freshSTU(freshX) = stu
			freshGPA(freshX) = GPA
			freshX = freshX + 1

		CASE "Sophomore"
			sophSTU(sophX) = stu
			sophGPA(sophX) = GPA
			sophX = sophX + 1

		CASE "Junior"
			juniSTU(juniX) = stu
			juniGPA(juniX) = GPA
			juniX = juniX + 1

		CASE "Senior"
			seniSTU(seniX) = stu
			seniGPA(seniX) = GPA
			seniX = seniX + 1

		END SELECT
	WEND
CLOSE #1 ' remember to close file too ;)

FOR x = 0 TO freshX
PRINT x, freshSTU(x), freshGPA(x)
NEXT x 

FOR x = 0 TO sophX
PRINT x, sophSTU(x), sophGPA(x)
NEXT x 

FOR x = 0 TO juniX
PRINT x, juniSTU(x), juniGPA(x)
NEXT x 

FOR x = 0 TO seniX
PRINT x, seniSTU(x), seniGPA(x)
NEXT x 
You see few bugs on code i did left there with purpose. Output of that program gives something extra from arrays. Now solve how you get riddof them ;)

[edit]
when you get your program running as you need, please post it here so we see what you ended with :)
[/edit]

TheWicker
Newbie
Posts: 5
Joined: Thu Oct 05, 2006 11:56 pm
Location: Tucson AZ
Contact:

Post by TheWicker » Mon Oct 09, 2006 7:53 pm

Thank you so much Lurah. I Finished the program there is just one last problem that I am having. I would like to print on either separate, or sequensial screens and haven't been able to find a command to do that, If there is one. Anyway here is the finished program. Also is there a better way to add all the nubers in an array to get the average than the way I did it here?


CLS
DIM stu AS STRING
DIM class AS STRING
DIM GPA AS DOUBLE

DIM freshSTU(30) AS STRING
DIM freshGPA(30) AS DOUBLE
DIM freshA AS DOUBLE
DIM freshB AS DOUBLE
DIM freshTemp AS DOUBLE
DIM freshTotal AS DOUBLE
DIM freshX AS INTEGER
freshX = 0

DIM sophSTU(30) AS STRING
DIM sophGPA(30) AS DOUBLE
DIM sophA AS DOUBLE
DIM sophB AS DOUBLE
DIM sophTemp AS DOUBLE
DIM sophTotla AS DOUBLE
DIM sophX AS INTEGER
sophX = 0

DIM juniSTU(30) AS STRING
DIM juniGPA(30) AS DOUBLE
DIM juniA AS DOUBLE
DIM juniB AS DOUBLE
DIM juniTemp AS DOUBLE
DIM juniTotal AS DOUBLE
DIM juniX AS INTEGER
juniX = 0

DIM seniSTU(30) AS STRING
DIM seniGPA(30) AS DOUBLE
DIM seniA AS DOUBLE
DIM seniB AS DOUBLE
DIM seniTemp AS DOUBLE
DIM SeniTemo AS DOUBLE
DIM seniX AS INTEGER
seniX = 0

DIM x AS INTEGER


OPEN "students.txt" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, stu, class, GPA

SELECT CASE class

CASE "Freshman"
freshSTU(freshX) = stu
freshGPA(freshX) = GPA
freshX = freshX + 1

CASE "Sophomore"
sophSTU(sophX) = stu
sophGPA(sophX) = GPA
sophX = sophX + 1

CASE "Junior"
juniSTU(juniX) = stu
juniGPA(juniX) = GPA
juniX = juniX + 1

CASE "Senior"
seniSTU(seniX) = stu
seniGPA(seniX) = GPA
seniX = seniX + 1

END SELECT
WEND
CLOSE #1



FOR freshA = 0 TO 29
FOR freshB = 0 TO freshA
IF freshGPA(freshB) < freshGPA(freshB + 1) THEN
freshTemp = freshGPA(freshB)
freshGPA(freshB) = freshGPA(freshB + 1)
freshGPA(freshB + 1) = freshTemp
END IF
NEXT freshB
NEXT freshA

PRINT
PRINT

FOR x = 0 TO freshX - 1
PRINT x, freshSTU(x), freshGPA(x)
NEXT x

x = 0
FOR x = 0 TO freshX - 1
freshTotal = freshGPA(x) + freshTotal
NEXT x

freshTotal = freshTotal / (freshX - 1)

PRINT
PRINT "Freshman Class Average",
PRINT USING "#.##"; freshTotal
SLEEP

FOR sophA = 0 TO 29
FOR sophB = 0 TO sophA
IF sophGPA(sophB) < sophGPA(sophB + 1) THEN
sophTemp = sophGPA(sophB)
sophGPA(sophB) = sophGPA(sophB + 1)
sophGPA(sophB + 1) = sophTemp
END IF
NEXT sophB
NEXT sophA

PRINT
PRINT

FOR x = 0 TO sophX - 1
PRINT x, sophSTU(x), sophGPA(x)
NEXT x
SLEEP

x = 0
FOR x = 0 TO sophX - 1
sophTotal = sophGPA(x) + sophTotal
NEXT x

sophTotal = sophTotal / (sophX - 1)

PRINT
PRINT "Sophomore Class Average",
PRINT USING "#.##"; sophTotal

FOR juniA = 0 TO 29
FOR juniB = 0 TO juniA
IF juniGPA(juniB) < juniGPA(juniB + 1) THEN
juniTemp = juniGPA(juniB)
juniGPA(juniB) = juniGPA(juniB + 1)
juniGPA(juniB + 1) = juniTemp
END IF
NEXT juniB
NEXT juniA

PRINT
PRINT

FOR x = 0 TO juniX - 1
PRINT x, juniSTU(x), juniGPA(x)
NEXT x
SLEEP

x = 0
FOR x = 0 TO juniX - 1
juniTotal = juniTotal + juniGPA(x)
NEXT x

juniTotal = juniTotal / (juniX - 1)

PRINT
PRINT "Junior Class Average",
PRINT USING "#.##"; juniTotal

PRINT
PRINT

FOR seniA = 0 TO 29
FOR seniB = 0 TO seniA
IF seniGPA(seniB) < seniGPA(seniB + 1) THEN
seniTemp = seniGPA(seniB)
seniGPA(seniB) = seniGPA(seniB + 1)
seniGPA(seniB + 1) = seniTemp
END IF
NEXT seniB
NEXT seniA

FOR x = 0 TO seniX - 1
PRINT x, seniSTU(x), seniGPA(x)
NEXT x
SLEEP

x = 0
FOR x = 0 TO seniX - 1
seniTotal = seniGPA(x) + seniTotal
NEXT x

seniTotal = seniTotal / (seniX - 1)

PRINT
PRINT "Senior Class Average",
PRINT USING "#.##"; seniTotal

[/quote]

bungytheworm
Veteran
Posts: 288
Joined: Sat Feb 18, 2006 4:02 pm

Post by bungytheworm » Tue Oct 10, 2006 9:26 am

Hi TheWicker.

I dont fully understand what you mean with
I would like to print on either separate, or sequensial screens
But what i look at your source (i cant test it atm since my dosbox got messed by some reason) youre on good road there.
Surely there is lot's of places where you could do things different, but to say honest, your program looks very good.

Keep on learning and youll find some lines, where you prolly want to make program run differently.

Personally i would like to do DIM like this.

Code: Select all

DIM freshSTU(30) AS STRING
DIM freshGPA(30) AS DOUBLE
DIM freshA, FreshB, FreshTemp, FreshTotal AS DOUBLE
DIM freshX AS INTEGER
      freshX = 0 
But your way is as workable than my one. No reason to change. But i think it's easier to read on way i do it. But thats totally opinion matter.

You could also try to use indent's on your code.

Code: Select all

WHILE NOT EOF(1)
	INPUT #1, stu, class, GPA

	SELECT CASE class
		CASE "Freshman"
			freshSTU(freshX) = stu
			freshGPA(freshX) = GPA
			freshX = freshX + 1

		CASE "Sophomore"
			sophSTU(sophX) = stu
			sophGPA(sophX) = GPA
			sophX = sophX + 1

		CASE "Junior"
			juniSTU(juniX) = stu
			juniGPA(juniX) = GPA
			juniX = juniX + 1

		CASE "Senior"
			seniSTU(seniX) = stu
			seniGPA(seniX) = GPA
			seniX = seniX + 1
	END SELECT

	WEND
CLOSE #1 
or

Code: Select all

FOR freshA = 0 TO 29
	FOR freshB = 0 TO freshA

		IF freshGPA(freshB) < freshGPA(freshB + 1) THEN
			freshTemp = freshGPA(freshB)
			freshGPA(freshB) = freshGPA(freshB + 1)
			freshGPA(freshB + 1) = freshTemp
		END IF

	NEXT freshB
NEXT freshA
Doesnt it look easier to follw?
One more thing i would recommend to you is comments.
Even your code aint longer than 192 lines and prolly it's very easy and simple to understand, for others (like me) it takes time to go thru it and get full picture of what it does and how.

Dont comment every line.

Code: Select all

FOR x = 0 TO sophX - 1
PRINT x, sophSTU(x), sophGPA(x)
NEXT x
SLEEP
Im sure you instantly now know what that part of code does. But others, it's a totally different thing.

Code: Select all

' Goes thru XXX because of reason XXX
FOR x = 0 TO sophX - 1
    PRINT x, sophSTU(x), sophGPA(x)
NEXT x
SLEEP
Short simple explanations on code, makes it way easier to read.

Post Reply