0

I need help with the code below. I coded using the For..Next statement, now I have to place the For..Next statement with a Do..Loop statement. Can anyone help down the right path? Thank you in advance.

`

        Dim strGrades() As String = {"A", "B", "C", "A", "B", "A", "F", "A", "D", "B", "C"}
        Dim strSearchFor As String
        Dim intCount As Integer


        strSearchFor = txtLetterGrade.Text.Trim.ToUpper
        intCount = 0



        For intSub As Integer = 0 To strGrades.Length - 1
            If strGrades(intSub) = strSearchFor Then
                intCount = intCount + 1

            End If
        Next intSub

        'Message box that displays the answer from the execution of the For Statement
        MessageBox.Show(strSearchFor & " : " &
                        intCount.ToString,
                        "Grades", MessageBoxButtons.OK,
                        MessageBoxIcon.Information)

`

6
Contributors
12
Replies
58
Views
4 Years
Discussion Span
Last Post by tinstaafl
Featured Replies
  • I'm pretty sure that won't work you might need to change it to this: Dim insSub As Integer = 0 Do Until intSub = strGrades.Length If strGrades(intSub) = strSearchFor Then intCount += 1 End If intSub += 1 Loop Read More

  • You make a string of valid grades such as Dim ValidGrades As String = "ABCDF" then you validate the input as If Instr(ValidGrades,strSearchFor) = 0 Then MsgBox("Invalid grade") Else 'do the search End If Read More

0

Does this help..i maybe way off grid with this

Do Until intSub <= strGrades.Length
            If strGrades(intSub) = strSearchFor Then
                intCount = intCount + 1

            End If
        Loop
        MessageBox.Show(strSearchFor & " : " &
                          intCount.ToString,
                          "Grades", MessageBoxButtons.OK,
                          MessageBoxIcon.Information)
1

I'm pretty sure that won't work you might need to change it to this:

Dim insSub As Integer = 0
Do Until intSub = strGrades.Length
    If strGrades(intSub) = strSearchFor Then
        intCount += 1
    End If
    intSub += 1
Loop
Votes + Comments
Thank you for that
0

You don't need a For loop or a Do loop or any kind of loop. The number you want is

intCount = Filter(strGrades,strSearchFor).Count

as long as you won't have other than single letter grades. For example, if your list of grades included "A B B A A+ A-" then the above would return 4 if strSearchFor was "A".

If you were told (for an assignment) to use a Do loop then the assignment is stupid. Nobody should teach you the wrong way to do something. Even a For loop with an index is silly (in this case). If you must use a loop then use

For Each grade As String In strGrades
    If grade = strSearchFor Then intCount += 1
Next

The above does an EXACT match to strSearchFor

Edited by Reverend Jim

0

While of very little practical value, an exercise like this, could be designed to encourage flexible thinking, and to get the student used to the idea of approaching a problem from different angles. Also the instructor may not have control over the curriculum and has to include an exercise using a Do-Loop.

0

Thank you tinstaafl. I see the error..I have a follow-up question on this if yall do not mind, hope robert dont mind...How would a code look for a message box that propmpts the user to enter a valid grades, in this case A through F. i want to expand my learning..This is what I have so far, so what I am missing?

If intSub = strGrades.Length Then
            MessageBox.Show(strSearchFor & " : " &
                            intCount.ToString,
                            "Grades", MessageBoxButtons.OK,
                            MessageBoxIcon.Information)
        Else
            MessageBox.Show("Enter a valid grade", "Grades",
                            MessageBoxButtons.OK, MessageBoxIcon.Information)

        End If

Edited by siaosituimoloaublood: edit comment

0

It is not prompting message "Enter a valid grade..everytime I give it a letter not in the array it stills give me a value..Like if I type in the letter G it gives me a value G:0..what i want to happen when a person does this is that a message box will inform the user to enter a valid code

1

You make a string of valid grades such as

Dim ValidGrades As String = "ABCDF"

then you validate the input as

If Instr(ValidGrades,strSearchFor) = 0 Then
    MsgBox("Invalid grade")
Else
    'do the search
End If
Votes + Comments
Awesomeness..it works..thank you for helping expand my knowledge
0

Since the Do loop is already indicating validation you can use that result like this:

Dim strGrades() As String = {"A", "B", "C", "A", "B", "A", "F", "A", "D", "B", "C"}
Dim strSearchFor As String
Dim intCount As Integer
strSearchFor = txtLetterGrade.Text.Trim.ToUpper
intCount = 0
Dim insSub As Integer = 0
Do Until intSub = strGrades.Length
    If strGrades(intSub) = strSearchFor Then
        intCount += 1
    End If
    intSub += 1
Loop
If intCount = 0 Then
    MessageBox.Show(strSearchFor & " : " & intCount.ToString, "Grades", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
    MessageBox.Show("Enter a valid grade", "Grades",  MessageBoxButtons.OK, MessageBoxIcon.Information)
End If

Edited by tinstaafl

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.