Matt.
Matt.

Reputation: 1294

For Loops And Arrays

I need a little help. My code is work 99% correctly except for one little thing.

I'm making what's called "Cafeteria Survey" which tallies responses from a ComboBox, which the user inputs them self.

The issue here is that it tallies (places a *) the number 1 less than the number I chose in the ComboBox.

If I add + 1 on the end of SelectedIndex it places the * with the correct number, but it doesn't do it for #10 responses(ratingComboBox.SelectedIndex) += 1

Any help would be fantastic. Thanks in advance.

Here's my code:

Public Class CafeteriaSurveyForm

Dim choices As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim responses(0 To 11) As Integer
Dim responseCounter As Integer = 0

' displays histogram
Sub DisplayHistogram()

    outputTextBox.Text = ("Rating" & vbTab & "Frequency")

    For i As Integer = 0 To choices.GetUpperBound(0)
        For ii As Integer = 1 To responses(i)
            outputTextBox.Text &= ("*")
        Next
        outputTextBox.Text &= (vbNewLine & choices(i) & vbTab)
    Next


End Sub ' DisplayHistogram

Private Sub CafeteriaSurveyForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    ratingComboBox.DataSource = choices
End Sub

Private Sub submitButton_Click(sender As System.Object, e As System.EventArgs) Handles submitButton.Click
    responseCounter += 1

    responses(ratingComboBox.SelectedIndex) += 1

    DisplayHistogram()

End Sub

End Class ' CafeteriaSurveyForm

Upvotes: 1

Views: 8441

Answers (3)

nickles80
nickles80

Reputation: 1101

Your display function is a little backward. Here is what you have now:

For i As Integer = 0 To choices.GetUpperBound(0)
    For ii As Integer = 1 To responses(i)
        outputTextBox.Text &= ("*")
    Next
    outputTextBox.Text &= (vbNewLine & choices(i) & vbTab)
Next

For every loop it is writing the asterisks to the previous line (because the new line is done after). If you increase the selected index it wrote them in the correct location but never got to write the asterisks for #10 because it exited the for loop before it got a chance.

This is what it should be:

For i As Integer = 0 To choices.GetUpperBound(0)
    outputTextBox.Text &= (vbNewLine & choices(i) & vbTab)
    For ii As Integer = 1 To responses(i)
        outputTextBox.Text &= ("*")
    Next
Next

Or even

For i As Integer = 0 To choices.GetUpperBound(0)
    outputTextBox.Text &= vbNewLine & choices(i) & vbTab & New String("*", responses(i))

Next

Now the choices and responses arrays are synced using the same indexes and are accessed during the same loop iteration.

Upvotes: 1

Alex Essilfie
Alex Essilfie

Reputation: 12613

Try this out to see if it gives you the expected output.

  1. Remove the responseCounter variable. It doesn't do anything significant (not any I see anyway).

  2. Change the responses declaration to Dim responses(9) As Integer.
    VB.NET uses zero (0) as the lower bound of an array (and almost every other type of collection) so the statement will create an array of integer with ten (10) elements.

  3. Change the For ii As Integer = 1 to responses(i) loop block to outputTextBox.Text &= New String('*', responses(i))

Upvotes: 0

emschorsch
emschorsch

Reputation: 1669

Maybe the error is that Responses(i) is defined to be integers in the range 1 to 11. If that range isn't inclusive that when you increment Responses(i) by 1 for the response 10 you will fall out of that range.

Upvotes: 0

Related Questions