Cneaus
Cneaus

Reputation: 5

Trying to create a search array but it does not exit loop in vb

Creating a Hangman game for a project and this bug comes up. storeWord value contains the word test in with t being in the first box, e being in the second box and so forth. When guess = t it displays correct but continues to without stopping.

This is my code;

            Do Until i = Len(targetWord)
                i = 1
                If guess = storeWord(i) Then
                    Console.WriteLine("Correct")
                    correctCount = correctCount + 1
                End If
                i = i + 1
            Loop

I want the loop to stop when it has search the whole array, the length of the word test, but when i run this it does not enter the loop. Help Thanks

Upvotes: 0

Views: 26

Answers (1)

ps2goat
ps2goat

Reputation: 8485

You keep setting i to 1.

       Do Until i = Len(targetWord)
            i = 1 ' <-- the culprit.
            If guess = storeWord(i) Then
                Console.WriteLine("Correct")
                correctCount = correctCount + 1
            End If
            i = i + 1
        Loop

Move your initialization of i outside the Do loop.

         i = 1
         Do Until i = Len(targetWord)
            If guess = storeWord(i) Then
                Console.WriteLine("Correct")
                correctCount = correctCount + 1
            End If
            i = i + 1
        Loop

Upvotes: 1

Related Questions