Results 1 to 3 of 3

Thread: Driving me crazy..

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    28

    Driving me crazy..

    This is a mathematics "testing" program, after every correct answer it increases the variable "userScore" by 1. This works perfectly along with the rest of the program, the only problem is that even when the answer the user inputs is WRONG and a message telling them they have got it wrong appears it STILL increments the "userScore" variable by 1.. I can't see whats wrong plaese help!

    Code:
    Public Class frmMultiplication
    
        'declare correctanswer as a variable which can be seen by the whole program
        Dim Correctanswer As Integer
        'diclare sub, multi, div, and add as variables seen by whole program
        Dim checkSub As Integer
        Dim checkMulti As Integer
        Dim checkDiv As Integer
        Dim checkAdd As Integer
    
        'declare userscore as variable which can be seen by the whole program 
        Dim userScore As Byte = 0
    
        Dim userattempts As Byte = 0
    
        Dim temp As Integer
    
        Private Sub frmMultiplication_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Randomize()
    
            'declare userinput as a variable
            Dim userinput As String
    
            'Set the userinput to equal the string inside the inputbox
            userinput = InputBox("Please enter your name")
    
            'Set the text property of the label equal to the string(welcome)plus the name variable
            lblUsername.Text = "Welcome, " + userinput
    
            btnNext.PerformClick()
    
        End Sub
    
        Dim randomnumber1 As Integer
        Dim randomnumber2 As Integer
    
        Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
            'Declare useranswer as a variable
            Dim useranswer As Integer
    
            'Initialize the random numberber generator'
            'generate random values between 1 and 4 for randomnumber1'
            randomnumber1 = CInt(Int((4 * Rnd() + 1)))
    
            'generate random values between 1 and 4 for randomnumber2
            randomnumber2 = CInt(Int((4 * Rnd() + 1)))
    
            'set the answer to equal the text in textbox1
            useranswer = Val(TextBox1.Text)
    
            'calculate score
            lblScore.Text = "You scored:" & Str(userScore) + " Correct answers"
    
            'Invalid character = msg box 
            If Not IsNumeric(TextBox1.Text) Then
                MsgBox("Please enter a number", vbOKOnly)
            End If
    
            'correct answer equals 1 0f 4 responces in blue text
            If useranswer = Correctanswer Then
                lblResponse1.ForeColor = Color.Blue
                If randomnumber1 = 1 Then
                    lblResponse1.Text = "Correct, Very good!            "
                End If
                If randomnumber1 = 2 Then
                    lblResponse1.Text = "Correct, Excellent!            "
                End If
                If randomnumber1 = 3 Then
                    lblResponse1.Text = "Correct, Nice Work!            "
                End If
                If randomnumber1 = 4 Then
                    lblResponse1.Text = "Correct, Keep up the good work!"
                End If
            End If
    
    
            'incorrect answer equals 1 0f 4 responces in red text
            If useranswer <> Correctanswer Then
                lblResponse1.ForeColor = Color.Red
                If randomnumber1 = 1 Then
                    lblResponse1.Text = "Wrong, Please try again.       "
                End If
                If randomnumber1 = 2 Then
                    lblResponse1.Text = "Wrong, Try once more.          "
                End If
                If randomnumber1 = 3 Then
                    lblResponse1.Text = "Wrong, Don't give up!          "
                End If
                If randomnumber1 = 4 Then
                    lblResponse1.Text = "Wrong, Keep trying.            "
                End If
    
            End If
    
            If useranswer <> Correctanswer Then
                userattempts = userattempts + 1
            End If
    
            If userattempts = 3 Then
                MsgBox("To many attempts")
                btnNext.PerformClick()
                userattempts = userattempts = 0
            End If
    
            If Correctanswer = randomnumber1 * randomnumber2 Then
                userattempts = 0
            ElseIf Correctanswer = randomnumber1 / randomnumber2 Then
                userattempts = 0
            ElseIf Correctanswer = randomnumber1 - randomnumber2 Then
                userattempts = 0
            ElseIf Correctanswer = randomnumber1 + randomnumber2 Then
                userattempts = 0
            End If
    
            'no text in textbox 1
            TextBox1.Text = ""
    
            'btnnext click to regenerate question
            btnNext.PerformClick()
        End Sub
    
        Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
            Close()
        End Sub
    
        Private Sub rbnSub_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbnSub.CheckedChanged
            'rbnsub is true 
            checkSub = 1
            checkAdd = 0
            checkDiv = 0
            checkMulti = 0
            btnNext.PerformClick()
    
        End Sub
    
        Private Sub rbnMulti_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbnMulti.CheckedChanged
            'rbnmulti is true
            checkSub = 0
            checkAdd = 0
            checkDiv = 0
            checkMulti = 1
            btnNext.PerformClick()
        End Sub
    
        Private Sub rbnAdd_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbnAdd.CheckedChanged
            'rbnadd is true
            checkSub = 0
            checkAdd = 1
            checkDiv = 0
            checkMulti = 0
            btnNext.PerformClick()
        End Sub
    
        Private Sub rbnDvision_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbnDvision.CheckedChanged
            'rbndivision is true
            checkSub = 0
            checkAdd = 0
            checkDiv = 1
            checkMulti = 0
            btnNext.PerformClick()
        End Sub
    
        Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
    
            'Check functions  = 1 when related rbn is clicked 
    
            If checkMulti = 1 Then
                randomnumber1 = CInt(Int((9 * Rnd() + 1)))
                randomnumber2 = CInt(Int((9 * Rnd() + 1)))
                lblQuestion.Text = "Question, " & Str(randomnumber1) & " x " & Str(randomnumber2)
                'equal number to the product of randomnumber1 and randomnumber2
                Correctanswer = randomnumber1 * randomnumber2
                If Correctanswer = randomnumber1 * randomnumber2 Then
                    userScore = userScore + 1
                End If
    
            ElseIf checkDiv = 1 Then
                randomnumber1 = CInt(Int((9 * Rnd() + 1)))
                randomnumber2 = CInt(Int((9 * Rnd() + 1)))
                lblQuestion.Text = "Question, " & Str(randomnumber1) & " / " & Str(randomnumber2)
                'equal number to the product of randomnumber1 and randomnumber2
                Correctanswer = randomnumber1 / randomnumber2
                If Correctanswer = randomnumber1 / randomnumber2 Then
                    userScore = userScore + 1
                End If
    
            ElseIf checkAdd = 1 Then
                randomnumber1 = CInt(Int((9 * Rnd() + 1)))
                randomnumber2 = CInt(Int((9 * Rnd() + 1)))
                lblQuestion.Text = "Question, " & Str(randomnumber1) & " + " & Str(randomnumber2)
                'equal number to the product of randomnumber1 and randomnumber2
                Correctanswer = randomnumber1 + randomnumber2
                If Correctanswer = randomnumber1 + randomnumber2 Then
                    userScore = userScore + 1
                End If
    
            ElseIf checkSub = 1 Then
                randomnumber1 = CInt(Int((9 * Rnd() + 1)))
                randomnumber2 = CInt(Int((9 * Rnd() + 1)))
                lblQuestion.Text = "Question, " & Str(randomnumber1) & " - " & Str(randomnumber2)
                'equal number to the product of randomnumber1 and randomnumber2
                Correctanswer = randomnumber1 - randomnumber2
                If Correctanswer = randomnumber1 - randomnumber2 Then
                    userScore = userScore + 1
                End If
    
            End If
    
        End Sub
    End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Driving me crazy..

    Take a look at this:
    Code:
                Correctanswer = randomnumber1 * randomnumber2
                If Correctanswer = randomnumber1 * randomnumber2 Then
                    userScore = userScore + 1
                End If
    You are incrementing userScore if Correctanswer is equal to (randomnumber1 * randomnumber2) but how could it possibly NOT equal that when that's exactly what you just set it to? The same goes for all your other calculations. You're always testing Correctanswer to see if it's the very value you just set it to, so it always will be.

    There's a serious flaw in your logic so I suggest that you forget the code for the moment and get your logic straight. Once you can execute the logic with pen and paper and get the correct result, then you can consider writing code to implement that logic.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Driving me crazy..

    Well the 3 IF statements that look like this right at the bottom of your code are always going to evaluate to true:
    vb Code:
    1. Correctanswer = randomnumber1 - randomnumber2
    2.             If Correctanswer = randomnumber1 - randomnumber2 Then
    3.                 userScore = userScore + 1
    4.             End If

    Your setting the CorrectAnswer variable to the product of your equation and then just testing to see if the product of your equation is equal to the CorrectAnswer variable... which of course it is because you just set it in the line right before the IF statement. I assume you should be doing something like this instead:
    vb Code:
    1. Correctanswer = randomnumber1 - randomnumber2
    2. If Correctanswer = AnswerTextBox.Text Then
    3.     userScore += 1
    4. End If
    Obviously thats just assuming that you have a textbox that the user's answer would be in.
    Also notice that I have changed the UserScore = UserScore + 1 statement to make it a little tider still does the exact same thing.

    Also, why are you declaring UserScore as Byte and not as Integer?


    EDIT: DAMMIT I TYPE TOO SLOW
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width