Keeping Score

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mblade
    New Member
    • Mar 2008
    • 1

    Keeping Score

    O.k. guys trying to teach myself in VB6 to start i am writing a math program for my daughter but i am having trouble with my code to keep score. the program is two random numbers that pop up in 2 txtboxes and a 3 txtbox for the answer on a 3 min timer what i want to do is keep track of how many she got right and display it at the end of the 3 min. here is the code any info you code give will be helpful and if you see any other mistakes in the code let me know.

    Code:
    Private Sub cmdcheck_Click()
    Dim score As Integer
    score = score + 1
    txtscore.Text = score
    Dim sum As String
    x = Answer.Text
    sum = val(text1.Text) + val(Text2.Text)
    
    If x = sum Then
    score = score + 1
    MsgBox "Good Job!"
    'put random number in textbox 1
    text1.Text = Int((10 - 0 + 1) * Rnd + 0)
    'put random number in textbox 2
    Text2.Text = Int((10 - 0 + 1) * Rnd + 0)
    'clear textbox 3 for the answer
    Answer.Text = ""
    
    Else
    
    MsgBox "wrong"
    ' put random number in textbox 1
    text1.Text = Int((10 - 0 + 1) * Rnd + 0)
    ' put random number in textbox 2
    Text2.Text = Int((10 - 0 + 1) * Rnd + 0)
    ' clear textbox 3 for the answer
    Answer.Text = ""
    End If
    End Sub
    as the code is it will add one to txt.score on the first right answer but then nothing after that. Thanks in advance for any help
  • jg007
    Contributor
    • Mar 2008
    • 283

    #2
    Originally posted by Mblade
    O.k. guys trying to teach myself in VB6 to start i am writing a math program for my daughter but i am having trouble with my code to keep score. the program is two random numbers that pop up in 2 txtboxes and a 3 txtbox for the answer on a 3 min timer what i want to do is keep track of how many she got right and display it at the end of the 3 min. here is the code any info you code give will be helpful and if you see any other mistakes in the code let me know.

    Code:
    Private Sub cmdcheck_Click()
    Dim score As Integer
    score = score + 1
    as the code is it will add one to txt.score on the first right answer but then nothing after that. Thanks in advance for any help
    you are using the score as an interer for this sub only and it is redimming it each time and setting it as 1

    sorry probably not described well but hopefully you understand what I mean, I would ( possibly incorrectly! ) be declaring score as an integer in the class section and then using the form load to set it to 0

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Ignore this message. I'm just "subscribin g" to the discussion thread so I can find it again later.

      Comment

      • jamesd0142
        Contributor
        • Sep 2007
        • 471

        #4
        Try this

        [code=text]
        Dim score As Integer = 0
        Dim x As String = Nothing


        Private Sub Form2_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
        'put random number in textbox 1
        Text1.Text = Int((10 - 0 + 1) * Rnd() + 0)
        'put random number in textbox 2
        Text2.Text = Int((10 - 0 + 1) * Rnd() + 0)
        'clear textbox 3 for the answer
        Answer.Text = ""
        End Sub

        Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
        Dim sum As String
        x = Answer.Text
        sum = Val(Text1.Text) + Val(Text2.Text)

        If x = sum Then
        score = score + 1
        MsgBox("Good Job!")
        'put random number in textbox 1
        Text1.Text = Int((10 - 0 + 1) * Rnd() + 0)
        'put random number in textbox 2
        Text2.Text = Int((10 - 0 + 1) * Rnd() + 0)
        'clear textbox 3 for the answer
        Answer.Text = ""

        Else
        MsgBox("wrong")
        ' put random number in textbox 1
        Text1.Text = Int((10 - 0 + 1) * Rnd() + 0)
        ' put random number in textbox 2
        Text2.Text = Int((10 - 0 + 1) * Rnd() + 0)
        ' clear textbox 3 for the answer
        Answer.Text = ""
        End If

        txtscore.Text = score
        End Sub
        [/code]

        NOTE: your 'random' numbers will repeat everytime u restart the program, to get better randoom numbers

        use something like this, to reduce the chance of repeating values.

        [code=text]
        Dim a As String
        a = System.DateTime .Now.Millisecon d
        [/code]


        MABY THIS WILL WORK UNLESS SOMEONE CAN SUGGEST ANY BETTER:

        [code=text]
        Dim score As Integer = 0
        Dim x As String = Nothing
        Dim a As String
        Dim b As String

        Private Sub Form2_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
        a = Mid(System.Date Time.Now.Millis econd, 1, 1)
        System.Threadin g.Thread.Sleep( CInt(a) + 284)
        b = Mid(System.Date Time.Now.Millis econd, 1, 1)
        'put random number in textbox 1
        Text1.Text = a 'Int((10 - 0 + 1) * Rnd() + 0)
        'put random number in textbox 2
        Text2.Text = b 'Int((10 - 0 + 1) * Rnd() + 0)
        'clear textbox 3 for the answer
        Answer.Text = ""
        End Sub

        Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
        a = Mid(System.Date Time.Now.Millis econd, 1, 1)
        System.Threadin g.Thread.Sleep( CInt(a) + 284)
        b = Mid(System.Date Time.Now.Millis econd, 1, 1)

        Dim sum As String
        x = Answer.Text
        sum = Val(Text1.Text) + Val(Text2.Text)

        If x = sum Then
        score = score + 1
        MsgBox("Good Job!")
        'put random number in textbox 1
        Text1.Text = a 'Int((10 - 0 + 1) * Rnd() + 0)
        'put random number in textbox 2
        Text2.Text = b 'Int((10 - 0 + 1) * Rnd() + 0)
        'clear textbox 3 for the answer
        Answer.Text = ""

        Else
        MsgBox("wrong")
        ' put random number in textbox 1
        Text1.Text = Int((10 - 0 + 1) * Rnd() + 0)
        ' put random number in textbox 2
        Text2.Text = Int((10 - 0 + 1) * Rnd() + 0)
        ' clear textbox 3 for the answer
        Answer.Text = ""
        End If

        txtscore.Text = score
        End Sub
        [/code]

        NOTE: i did this in vb2005 but should work in vb6

        Comment

        • jg007
          Contributor
          • Mar 2008
          • 283

          #5
          as far as I am aware RND() generates a random value based on system time/data so should not be the same although I did used to use randomise at the start of programs but I am not sure if that was necessary

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by jg007
            as far as I am aware RND() generates a random value based on system time/data so should not be the same although I did used to use randomise at the start of programs but I am not sure if that was necessary
            In VB6, you need to use the Randomize statement to seed the pseudo-random number generator. If you don't, it will produce the same series of numbers each time. I think Randomize uses the system time, unless you provide a seed.

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Originally posted by jamesd0142
              ...Int((10 - 0 + 1) * Rnd() + 0)
              Is this being done to illustrate what is normally added/subtracted in these circumstances, or what? Because it seems as though you could get the same result with Int(11 * Rnd()).

              Comment

              Working...