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
Sub Script out of range
Sub Script out of range
I am getting a Sub Script out of range error on this, any one know how to fix it?
-
- Veteran
- Posts: 288
- Joined: Sat Feb 18, 2006 4:02 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
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
-
- Veteran
- Posts: 399
- Joined: Wed Mar 02, 2005 9:01 pm
- Location: Nashville, Tennessee
- Contact:
I think the problem is the following:
Before the while, you need to set x=0,
otherwise the while will start off with x equal to what it was before.
*****
Code: Select all
while x < 30
print freshSTU(x), freshGPA(x)
x = x + 1
wend
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.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
-
- Veteran
- Posts: 288
- Joined: Sat Feb 18, 2006 4:02 pm
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
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
-
- Veteran
- Posts: 288
- Joined: Sat Feb 18, 2006 4:02 pm
Code: Select all
x = 0
FOR x = 0 TO 100
PRINT x, sophSTU(x), sophGPA(x)
x = x + 1
SLEEP
NEXT x
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
[edit]
when you get your program running as you need, please post it here so we see what you ended with
[/edit]
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]
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
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 "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
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 "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
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 "Junior Class Average",
PRINT USING "#.##"; juniTotal
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 "Senior Class Average",
PRINT USING "#.##"; seniTotal
[/quote]
-
- Veteran
- Posts: 288
- Joined: Sat Feb 18, 2006 4:02 pm
Hi TheWicker.
I dont fully understand what you mean with
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.
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.
or
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.
Im sure you instantly now know what that part of code does. But others, it's a totally different thing.
Short simple explanations on code, makes it way easier to read.
I dont fully understand what you mean with
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.I would like to print on either separate, or sequensial screens
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
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
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
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
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