CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Apr 2006
    Posts
    5

    Unhappy 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

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    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

  3. #3
    Join Date
    Mar 2002
    Location
    Croatia
    Posts
    275

    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.

  4. #4
    Join Date
    Apr 2006
    Posts
    5

    Re: Inadvertent looping!

    Quote 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"

  5. #5
    Join Date
    Mar 2002
    Location
    Croatia
    Posts
    275

    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.

  6. #6
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    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))

  7. #7
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    Re: Inadvertent looping!

    Quote 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.

  8. #8
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    Re: Inadvertent looping!

    Quote 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.

  9. #9
    Join Date
    Nov 2005
    Location
    Omaha, Nebraska, USA
    Posts
    696

    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.

  10. #10
    Join Date
    Nov 2005
    Location
    Omaha, Nebraska, USA
    Posts
    696

    Re: Inadvertent looping!

    Quote 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.

  11. #11
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    Re: Inadvertent looping!

    Quote 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
    Quote 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.

  12. #12
    Join Date
    Apr 2006
    Posts
    5

    Re: Inadvertent looping!

    Exactly like the second example!


    Quote 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.

  13. #13
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    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

  14. #14
    Join Date
    Apr 2006
    Posts
    5

    Re: Inadvertent looping!

    COMINTERN,
    That did it. thanks a million. Now I have to look at it carefully and figure out how it worked.

  15. #15
    Join Date
    Apr 2006
    Posts
    5

    Post 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!

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured