John Does
John Does

Reputation: 19

Visual Basic Loop Issue

Is anyone familiar with the Bacon number?

Now is anyone familiar with the notion that when you spell out a number, its value is 4. For example, 3 is "three." "Three" has 5 letters. "Five" has 4. Every number will lead to four. I am making a program that traces how many steps it takes to get to four.

I am currently doing pretty well, but I can's seem to re run the program without it freezing, ultimately signalling it is in an infinite loop. This is the code.

Public Class Form1
Dim intInput, intNumber, intCounter, intFinalCount As Integer
Dim strWord As String
Function count()
    For Each singleChar In strWord
        intCounter += 1
    Next
    Select Case intCounter
        Case 1
            strWord = "one"
        Case 2
            strWord = "two"
        Case 3
            strWord = "three"
        Case 4
            strWord = "four"
        Case 5
            strWord = "five"
        Case 6
            strWord = "six"
    End Select
End Function
Function count2()
    Select Case intCounter
        Case 1
            strWord = "one"
        Case 2
            strWord = "two"
        Case 3
            strWord = "three"
        Case 4
            strWord = "four"
        Case 5
            strWord = "five"
        Case 6
            strWord = "six"
    End Select
End Function
Private Sub btnPomeranz_Click(sender As Object, e As EventArgs) Handles btnPomeranz.Click
    intInput = 0
    intNumber = 0
    intFinalCount = 0
    strWord = ""
    intCounter = 0
    If txtKershaw.Text <> "" Then
        intInput = txtKershaw.Text
    Else
        Close()
    End If
    intNumber = intInput

    count2()
    count()


    Do While intCounter <> 4
        If intCounter <> 4 Then
            count()
            intFinalCount += 1
        End If
    Loop

    lblKluber.Text = intFinalCount

End Sub
End Class

Upvotes: 0

Views: 407

Answers (3)

Blackwood
Blackwood

Reputation: 4534

You can simplify your code and eliminate the error like this:

Public Class Form1
    Private numbers As New List(Of String) From {"Zero", "One", "Two", "Three", "Four", _
      "Five", "Six", "Seven", "Eight", "Nine", "Ten"}

    Private Sub btnPomeranz_Click(sender As Object, e As EventArgs) _
      Handles btnPomeranz.Click
        Dim number, bacon As Integer

        If Not Integer.TryParse(txtKershaw.Text, number) Then
            MessageBox.Show(txtKershaw.Text & " is not an integer")
            Exit Sub
        End If

        Do
            number = Count(number)
            If number = 0 Then
                MessageBox.Show("The number " & txtKershaw.Text & " was not recognised")
                Exit sub
            End If

            bacon += 1
            If bacon > 100 Then
                MessageBox.Show("More than 100 steps required")
                Exit Sub
            End If
        Loop Until number = 4
        lblKluber.Text = bacon.ToString
    End Sub

    Private Function Count(number As Integer) As Integer
        If number < 0 Or number >= numbers.Count Then Return 0
        Return numbers(number).Length
    End Function
End Class

Note that this code uses functions the way they were intended, you pass input to them as arguments, and the function returns the output using the Return statement. The Count function

  • Takes the current number as its argument.
  • Returns 0 if the number is invalid (not between 0 and 10).
  • Otherwise returns the length of the word representing the number.

We use a List(Of String) to hold the names of the valid numbers. That makes it easy to check if a number is within range, it must be >=0 and < the length of the list (note that list items are numbered starting from 0). It also makes it easy to look up the name of a number (name = numbers(number)).

The button click handler simply needs to check that the input TextBox contains an integer (Integer.TryParse does that as well as converting the text into a number). Then it calls Count in a loop until the answer 4 is found. In case of problems with the code, the loop exits after 100 tries (to avoid an infinite loop).

Upvotes: 2

Chillzy
Chillzy

Reputation: 468

There look for the difference in your code and my code and look at this to learn how to exit an app safely

you need to reset your count here to 0.

   Function count()
        intCounter = 0
        For Each singleChar In strWord
            intCounter += 1
        Next

it should read intNumber instead of intCounter

Function count2()
    Select Case intCounter
        Case 1

This will only close the form and leave your app in the background. use End instead

 If txtKershaw.Text <> "" Then
        intInput = txtKershaw.Text
    Else
        Application.Exit ' this is the correct way of closing your app. End() makes a hard exit and close() will never close your app until the thread is close
end if

Working code

Public Class Form1
    Dim intInput, intNumber, intCounter, intBacon As Integer
    Dim strWord As String

    Private Sub btnPomeranz_Click_1(sender As Object, e As EventArgs) Handles btnPomeranz.Click
        intInput = 0
        intNumber = 0
        intBacon = 0
        strWord = ""
        intCounter = 0
        If txtKershaw.Text <> "" Then
            intInput = txtKershaw.Text
        Else
            End
        End If
        intNumber = intInput
        count2()
        count()


        Do While intCounter <> 4
            If intCounter <> 4 Then
                count()
                intBacon += 1
            End If
        Loop

        lblKluber.Text = intBacon
    End Sub

    Function count()
        intCounter = 0
        For Each singleChar In strWord
            intCounter += 1
        Next
        Select Case intCounter
            Case 1
                strWord = "one"
            Case 2
                strWord = "two"
            Case 3
                strWord = "three"
            Case 4
                strWord = "four"
            Case 5
                strWord = "five"
            Case 6
                strWord = "six"
        End Select
    End Function
    Function count2()
        Select Case intNumber
            Case 1
                strWord = "one"
            Case 2
                strWord = "two"
            Case 3
                strWord = "three"
            Case 4
                strWord = "four"
            Case 5
                strWord = "five"
            Case 6
                strWord = "six"
        End Select
    End Function
End Class

Upvotes: 2

SE1986
SE1986

Reputation: 2740

Your problem lies in the fact that after the inital value assignment of intCounter:

intCounter = 0

intCounter is not getting set anywhere else and will always remain as 0, therefore

Do While intCounter <> 4
    If intCounter <> 4 Then
        count()
        intBacon += 1
    End If
Loop

will loop indefinitely

The only place it could get set is here

For Each singleChar In strWord
    intCounter += 1
Next

butstrWord is never set after its initial assignment of "" either so intCounter += 1 will never execute

As a side, you should only have one count() function and pass parameters to it and have it return the result.

There are a fair few other issues with this code but that is outside the scope of the question

Upvotes: 2

Related Questions