So I have this assignment for school and I've got the program working, but some minor issues (maybe major issues with cleanilness of code, but working on it). I'm having to make a Guess My Number program and it seems to work, but when I start a new game it wants to check the guess against the old answer instead of the new random number. I will supply what my code is and the instructions of the project.

Public Class Form1

    'module-level scope variables
    Dim guessLimit As Single = 0
    Dim correctGuesses As Single = 0
    Dim totalGuesses As Single = 0
    Dim randomNumber As Single


    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click

        Dim averageCorrect As Single

        'calculate users avg of correct guesses
        averageCorrect = (correctGuesses / totalGuesses)

        MessageBox.Show("You had " & correctGuesses & " correct guesses out of 5 guesses, for a win percentage of " _
                        & averageCorrect.ToString("P"))


        'exit application
        Me.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim generateRandomNumber As New Random


        'Generate a random number for user to guess
        randomNumber = generateRandomNumber.Next(1, 10)


    End Sub

    Private Sub checkGuessButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkGuessButton.Click

        'users guess
        Dim guess As Single
        'Dim answer As Single

        'answer = randomNumber


        If Not Single.TryParse(guessTextBox.Text, guess) Then
            MessageBox.Show("You must enter a numeric value here.", "Bad value", _
                             MessageBoxButtons.OK, MessageBoxIcon.Information)

            'setup textbox
            setup()

            Exit Sub
        End If

        'Single.TryParse(guessTextBox.Text, guess)

        If guess = randomNumber Then
            MessageBox.Show("Congratulations! You guessed my number! Click on Start New Game to play again.", "Winner", _
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            'increment total and correct guesses on A CORRECT GUESS
            correctGuesses += 1
            totalGuesses += 1

        Else

            If totalGuesses < 5 Then
                MessageBox.Show("Sorry! Try another guess.", "Incorrect", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                guessLimit += 1
                totalGuesses += 1
                setup()


            ElseIf totalGuesses > 5 Then
                MessageBox.Show("Sorry! You have used all 5 chances. I have a new number. Try to guess it.", "New game", _
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                'clear the textbox
                setup()
                'reset guess count limitor
                guessLimit = 0



                'Generate a new random number
                Dim generateRandomNumber As New Random
                Dim randomNumber As Single



                'Generate a random number for user to guess
                randomNumber = generateRandomNumber.Next(1, 10)

                'For testing purposes ONLY remove or comment out
                guessTextBox.Text = randomNumber.ToString()

            End If


        End If


    End Sub

    Private Sub newGameButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newGameButton.Click

        newGame()

    End Sub

    'sub routine to clear textbox and set it to focus
    Sub setup()
        guessTextBox.Clear()
        guessTextBox.Focus()
    End Sub


    Sub newGame()

        'clear the textbox
        setup()
        'reset guess count limitor
        guessLimit = 0

        'Generate a new random number
        Dim generateRandomNumber As New Random
        Dim randomNumber As Single



        'Generate a random number for user to guess
        randomNumber = generateRandomNumber.Next(1, 10)

    End Sub

End Class

I will paste the instructions as a reply to this..

Thank you...I've been banging my head against the wall on this.

In your Sub newGame() You are Dim(ing) a variable named randomNumber instead of just reassigning your global variable new value. Since it's scope is local to only the Sub newGame it will have no effect on the global variable randomNumber and it will be whatever you assigned it in the beginning.

I've submitted the code to teach and what I had changed is below.. Seems to be working smoothly.

Public Class Form1

    'module-level scope variables
    Dim guessLimit As Single = 0
    Dim correctGuesses As Single = 0
    Dim totalGuesses As Single = 0
    Dim randomNumber As Single
    Dim generateRandomNumber As New Random


    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click

        Dim averageCorrect As Single

        'calculate users avg of correct guesses
        averageCorrect = (correctGuesses / totalGuesses)

        MessageBox.Show("You had " & correctGuesses & " correct guesses out of 5 guesses, for a win percentage of " _
                        & averageCorrect.ToString("P"))


        'exit application
        Me.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        'Generate a random number for user to guess
        randomNumber = generateRandomNumber.Next(1, 10)

    End Sub

    Private Sub checkGuessButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkGuessButton.Click

        'users guess
        Dim guess As Single


        If Not Single.TryParse(guessTextBox.Text, guess) Then
            MessageBox.Show("You must enter a numeric value here.", "Bad value", _
                             MessageBoxButtons.OK, MessageBoxIcon.Information)

            'setup textbox
            setup()

            Exit Sub
        End If


        If guess = randomNumber Then
            MessageBox.Show("Congratulations! You guessed my number! Click on Start New Game to play again.", "Winner", _
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            'increment total and correct guesses on A CORRECT GUESS
            correctGuesses += 1
            totalGuesses += 1

        Else

            If guessLimit < 5 Then
                MessageBox.Show("Sorry! Try another guess.", "Incorrect", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                guessLimit += 1
                totalGuesses += 1
                setup()

            Else
                MessageBox.Show("Sorry! You have used all 5 chances. I have a new number. Try to guess it.", "New game", _
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                newGame()

            End If

        End If

    End Sub

    Private Sub newGameButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newGameButton.Click

        newGame()

    End Sub

    'sub routine to clear textbox and set it to focus
    Sub setup()
        guessTextBox.Clear()
        guessTextBox.Focus()
    End Sub


    Sub newGame()

        guessTextBox.Clear()
        guessTextBox.Focus()

        'reset guess count limitor
        guessLimit = 0


        'Generate a random number for user to guess
        randomNumber = generateRandomNumber.Next(1, 11)

    End Sub

End Class

If your question has been answered satisfactorily please mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.