|
-
April 26th, 2006, 08:42 AM
#1
Inadvertent looping!
I'm a beginning student. Can someone tell me why this is looping each name for 3 rows in a list box? The file it's reading from is at the bottom.
Thanks
'DECLARE VARIABLES
Dim names(5) As String
Dim SCORES(5, 2) As Integer
Dim row, col As Integer
Dim ave(5) As Double
Dim grade(5) As String
Dim sum As Integer
Dim tstave As Integer
Private Sub CALC_Click()
lstdisplay.AddItem ("NAME - SCORES - AVERAGE - GRADE")
'OPEN FILE
Open "F:\DATAIN3.TXT" For Input As #1
Open "F:\DATAOUT3.TXT" For Output As #2
'GET DATA
For row = 0 To 5
Input #1, names(row)
sum = 0
For col = 0 To 2
Input #1, SCORES(row, col)
Next 'ROW
Next 'COL
For row = 0 To 5
sum = 0
For col = 0 To 2
'CALCULATE SUM OF SCORES
sum = sum + SCORES(row, col)
'CALCULATE AVERAGE OF SCORES
ave(row) = Math.Round((sum / 3), 2)
'CALCULATE LETTER GRADE
If ave(row) > 90 <= 100 Then grade(col) = "A" Else
If ave(row) > 80 < 90 Then grade(col) = "B" Else
If ave(row) > 70 < 80 Then grade(col) = "C" Else
If ave(row) > 60 < 70 Then grade(col) = "D" Else
If ave(row) < 60 Then grade(col) = "F"
'DISPLAY RESULTS
lstdisplay.AddItem (names(row) & " " & SCORES(row, col) & " " & ave(row) & " " & grade(row))
Print #2, names(row), SCORES(row, col)
Next 'ROW
Next 'COL
End Sub
Private Sub Command2_Click()
lstdisplay.Clear
End Sub
Private Sub EXIT_Click()
End
End Sub
datain3.txt:
ART
86
77
98
MARY
67
91
74
JILL
87
77
82
MARK
96
89
91
SAM
65
59
52
CAROL
89
85
71
-
April 26th, 2006, 08:54 AM
#2
Re: Inadvertent looping!
move
DISPLAY RESULTS
lstdisplay.AddItem (names(row) & " " & SCORES(row, col) & " " & ave(row) & " " & grade(row))
Print #2, names(row), SCORES(row, col)
To below
Next 'ROW
-
April 26th, 2006, 08:59 AM
#3
Re: Inadvertent looping!
You have two nested loops:
Code:
For row = 0 To 5
For col = 0 To 2
...
'DISPLAY RESULTS
lstdisplay.AddItem (names(row) & " " & SCORES(row, col) & ...
Next 'ROW
Next 'COL
Your end-comment is not correct, in fact you should write:
Code:
lFor row = 0 To 5
For col = 0 To 2
...
'DISPLAY RESULTS
lstdisplay.AddItem (names(row) & " " & SCORES(row, col) & ...
Next 'COL
Next 'ROW
but it works, since you have commented varable names ROW and COL.
As you can see, for each row you invoke AddItem 3 times - for each COL once.
-
April 26th, 2006, 09:46 AM
#4
Re: Inadvertent looping!
 Originally Posted by sotoasty
move
DISPLAY RESULTS
lstdisplay.AddItem (names(row) & " " & SCORES(row, col) & " " & ave(row) & " " & grade(row))
Print #2, names(row), SCORES(row, col)
To below
Next 'ROW
I understand your concept, but when I do this I get "Run-Time error '9': Subscript out of range"
-
April 26th, 2006, 10:03 AM
#5
Re: Inadvertent looping!
You have defined Names field:
Dim names(5) As String
but you loop from 0 to 5, that means Names field must be defined as:
Dim names(6) As String
Check also other fields.
-
April 26th, 2006, 10:12 AM
#6
Re: Inadvertent looping!
It looks like you're using the wrong index for 'grade'. Shouldn't these:
Code:
lstdisplay.AddItem (names(row) & " " & SCORES(row, col) & " " & ave(row) & " " & grade(row))
be like this?
Code:
lstdisplay.AddItem (names(row) & " " & SCORES(row, col) & " " & ave(row) & " " & grade(col))
-
April 26th, 2006, 10:13 AM
#7
Re: Inadvertent looping!
 Originally Posted by ergas
You have defined Names field:
Dim names(5) As String
but you loop from 0 to 5, that means Names field must be defined as:
Dim names(6) As String
Check also other fields.
That would only be true if he was using Option Base 1. The number of elements defaults to the 0 base UBound, so names(5) is correct.
-
April 26th, 2006, 10:18 AM
#8
Re: Inadvertent looping!
 Originally Posted by smcoh326
I understand your concept, but when I do this I get "Run-Time error '9': Subscript out of range"
The subscript out of range comes from indexing with a For-Next variable outside of the loop. When your code crosses the 'Next' statement, it always adds the step value. So, the last time through it will still increment it even though the loop code doesn't execute. Try this to see what I'm talking about:
Code:
Dim Row As Integer
For Row = 0 To 5
Debug.Print Row
Next Row 'Increments, even on the last time through
Debug.Print Row 'Row is 6 here, which is an overbound.
-
April 26th, 2006, 10:26 AM
#9
Re: Inadvertent looping!
The issue isn't his Names array. Since he isn't defining an Option Base 1 and his code worked fine before, we know it is setting it as (0 To 5), not (0 To 4) or (1 To 5).
The line in question that gets the "Subscript out of range" error is:
Code:
lstdisplay.AddItem (names(row) & " " & SCORES(row, col) & " " & ave(row) & " " & grade(row))
At that point, col = 3. The For loop sets col to 3, then exits it.
My question is how do you want to see the output? Do you want to see all the scores in the item or just one? Like (barring formatting):
Code:
NAME - SCORES - AVERAGE - GRADE
ART 98 87 B
MARY 74 77.33 C
JILL 82 82 B
MARK 91 92 A
SAM 52 58.67 F
CAROL 71 81.67 B
Or:
Code:
NAME - SCORES - - AVERAGE - GRADE
ART 86 77 98 87 B
MARY 67 91 74 77.33 C
JILL 87 77 82 82 B
MARK 96 89 91 92 A
SAM 65 59 52 58.67 F
CAROL 89 85 71 81.67 B
Or do you even need to see the scores?
Once we know what formatting you're looking for, we can help you solve your problem.
-
April 26th, 2006, 10:34 AM
#10
Re: Inadvertent looping!
 Originally Posted by Comintern
It looks like you're using the wrong index for 'grade'.
Actually, I think using row in grade() is fine, since grade is defined as:
Code:
Dim grade(5) As String
And col only goes from 0 to 2.
The problem comes when the OP goes to "calculate the letter grade". At that point, it should be changed to:
Code:
If (ave(row) > 90 And ave(row) <= 100) Then grade(row) = "A" Else
If (ave(row) > 80 And ave(row) < 90) Then grade(row) = "B" Else
If (ave(row) > 70 And ave(row) < 80) Then grade(row) = "C" Else
If (ave(row) > 60 And ave(row) < 70) Then grade(row) = "D" Else
If ave(row) < 60 Then grade(row) = "F"
@smcoh326
Doing the comparison checks as you did:
Code:
ave(row) > 90 <= 100
Returns True, though I don't know why. That caused it to set the value incorrectly each time.
I didn't know you could do If/Then/Else like that, though.
-
April 26th, 2006, 10:47 AM
#11
Re: Inadvertent looping!
 Originally Posted by ChaosTheEternal
Actually, I think using row in grade() is fine, since grade is defined as:
Code:
Dim grade(5) As String
And col only goes from 0 to 2.
I was having the same problem trying to figure out the formatting
 Originally Posted by ChaosTheEternal
I didn't know you could do If/Then/Else like that, though.
It's actually just a single line If statement, with no else condition like:
Code:
If False Then Debug.Print "FALSE" Else
'EQUALS
If False Then
Debug.Print "FALSE"
Else
End If
It is kind of funky though, never seen it before either.
-
April 26th, 2006, 02:15 PM
#12
Re: Inadvertent looping!
Exactly like the second example!
 Originally Posted by ChaosTheEternal
The issue isn't his Names array. Since he isn't defining an Option Base 1 and his code worked fine before, we know it is setting it as (0 To 5), not (0 To 4) or (1 To 5).
The line in question that gets the "Subscript out of range" error is:
Code:
lstdisplay.AddItem (names(row) & " " & SCORES(row, col) & " " & ave(row) & " " & grade(row))
At that point, col = 3. The For loop sets col to 3, then exits it.
My question is how do you want to see the output? Do you want to see all the scores in the item or just one? Like (barring formatting):
Code:
NAME - SCORES - AVERAGE - GRADE
ART 98 87 B
MARY 74 77.33 C
JILL 82 82 B
MARK 91 92 A
SAM 52 58.67 F
CAROL 71 81.67 B
Or:
Code:
NAME - SCORES - - AVERAGE - GRADE
ART 86 77 98 87 B
MARY 67 91 74 77.33 C
JILL 87 77 82 82 B
MARK 96 89 91 92 A
SAM 65 59 52 58.67 F
CAROL 89 85 71 81.67 B
Or do you even need to see the scores?
Once we know what formatting you're looking for, we can help you solve your problem.
-
April 26th, 2006, 02:24 PM
#13
Re: Inadvertent looping!
Try this:
Code:
Dim sScoreDisp As String 'Add this
Private Sub CALC_Click()
lstdisplay.AddItem ("NAME - SCORES - AVERAGE - GRADE")
'OPEN FILE
Open "F:\DATAIN3.TXT" For Input As #1
Open "F:\DATAOUT3.TXT" For Output As #2
'GET DATA
For row = 0 To 5
Input #1, names(row)
For col = 0 To 2
Input #1, SCORES(row, col)
Next col
Next row
For row = 0 To 5
sum = 0
For col = 0 To 2
'CALCULATE SUM OF SCORES
sum = sum + SCORES(row, col)
Next col
'CALCULATE AVERAGE OF SCORES
ave(row) = Math.Round((sum / 3), 2)
'CALCULATE LETTER GRADE
If ave(row) > 90 <= 100 Then grade(row) = "A"
If ave(row) > 80 < 90 Then grade(row) = "B"
If ave(row) > 70 < 80 Then grade(row) = "C"
If ave(row) > 60 < 70 Then grade(row) = "D"
If ave(row) < 60 Then grade(row) = "F"
'Build the scores to display.
sScoreDisp = SCORES(row, 0) & " " & SCORES(row, 1) & " " & SCORES(row, 2)
'DISPLAY RESULTS
lstdisplay.AddItem (names(row) & " " & sScoreDisp & " " & ave(row) & " " & grade(row))
Print #2, names(row), sScoreDisp
Next row
End Sub
-
April 26th, 2006, 03:28 PM
#14
Re: Inadvertent looping!
COMINTERN,
That did it. thanks a million. Now I have to look at it carefully and figure out how it worked.
-
April 26th, 2006, 03:47 PM
#15
Re: Inadvertent looping!
I also replaced:
'CALCULATE LETTER GRADE
If ave(row) > 90 <= 100 Then grade(col) = "A" Else
If ave(row) > 80 < 90 Then grade(col) = "B" Else
If ave(row) > 70 < 80 Then grade(col) = "C" Else
If ave(row) > 60 < 70 Then grade(col) = "D" Else
If ave(row) < 60 Then grade(col) = "F"
With:
'CALCULATE LETTER GRADE
If ave(row) >= 90 And ave(row) <= 100 Then
grade(row) = "A"
ElseIf ave(row) >= 80 And ave(row) <= 89 Then
grade(row) = "B"
ElseIf ave(row) >= 70 And ave(row) <= 79 Then
grade(row) = "C"
ElseIf ave(row) >= 60 And ave(row) <= 69 Then
grade(row) = "D"
ElseIf ave(row) < 60 Then grade(row) = "F"
End If
Otherwise, everyone got an F!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|