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
Your display function is a little backward. Here is what you have now:
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:
Or even
Now the choices and responses arrays are synced using the same indexes and are accessed during the same loop iteration.