# Can you please tell me what I have done wrong here!!!!

Hello....again! I almost have the below program working correctly, but not quite...It is suppose to display the mult. problem and then the user enters their answer and clicks the button. If the answer is correct, it displays a good job message and displays a new problem. If they are wrong, it says try again and the problem remains the same until it is answered correctly. I have it now so that it recognizes the difference between right and wrong answers, but it will not generate a new problem. I am sure there is something wrong with the loops somewhere, but I can't find the mistake!!!! Thanks for any help you can give me! Linda

Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click

Dim number1, number2, userGuess As Integer
Dim i As String

number1 = value1.Text
number2 = value2.Text
userGuess = InputBox.Text

CheckCalculation(number1, number2, userGuess)

If i = 2 Then
GenerateNewNumber()
Else
CheckCalculation(number1, number2, userGuess)
End If

End Sub

Function CheckCalculation(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As String

Dim reply As String = "That is right, great job! Clear the box and try the next one!"
Dim reply1 As String = "Sorry, please clear the box and try again!"
Dim returned As Integer

If z <> x * y Then
Else
Return 2
End If

End Function

Function GenerateNewNumber()

Dim randomObject As Random = New Random()
Dim randomNumber As Integer = randomObject.Next()

'two random numbers being multiplied
Dim number1 As Integer
Dim number2 As Integer

Dim userGuess As Integer

'random numbers between 0 and 9 are generated
number1 = randomObject.Next(0, 10)
number2 = randomObject.Next(0, 10)

'assigns appropriate variable to textboxes for display
Me.value1.Text = String.Format("{0}", number1)
Me.value2.Text = String.Format("{0}", number2)

'converts numbers to be mult. to integers
number1 = Convert.ToDouble(Me.value1.Text)
number2 = Convert.ToDouble(Me.value2.Text)

End Function

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

GenerateNewNumber()

End Sub

End Class

• [b][red]This message was edited by Fallen at 2002-11-7 14:58:44[/red][/b][hr]
: Hello....again! I almost have the below program working correctly, but not quite...It is suppose to display the mult. problem and then the user enters their answer and clicks the button. If the answer is correct, it displays a good job message and displays a new problem. If they are wrong, it says try again and the problem remains the same until it is answered correctly. I have it now so that it recognizes the difference between right and wrong answers, but it will not generate a new problem. I am sure there is something wrong with the loops somewhere, but I can't find the mistake!!!! Thanks for any help you can give me! Linda
:
: Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
:
: Dim number1, number2, userGuess As Integer
: Dim i As String
:
: number1 = value1.Text
: number2 = value2.Text
: userGuess = InputBox.Text
:
: 'call to check user's answer
: CheckCalculation(number1, number2, userGuess)
:
: If i = 2 Then
: GenerateNewNumber()
: Else
: CheckCalculation(number1, number2, userGuess)
: End If
:
: End Sub
:
: Function CheckCalculation(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As String
:
: Dim reply As String = "That is right, great job! Clear the box and try the next one!"
: Dim reply1 As String = "Sorry, please clear the box and try again!"
: Dim returned As Integer
:
: If z <> x * y Then
: Else
: Return 2
: End If
:
:
: End Function
:
: Function GenerateNewNumber()
:
: Dim randomObject As Random = New Random()
: Dim randomNumber As Integer = randomObject.Next()
:
: 'two random numbers being multiplied
: Dim number1 As Integer
: Dim number2 As Integer
:
: Dim userGuess As Integer
:
: 'random numbers between 0 and 9 are generated
: number1 = randomObject.Next(0, 10)
: number2 = randomObject.Next(0, 10)
:
: 'assigns appropriate variable to textboxes for display
: Me.value1.Text = String.Format("{0}", number1)
: Me.value2.Text = String.Format("{0}", number2)
:
: 'converts numbers to be mult. to integers
: number1 = Convert.ToDouble(Me.value1.Text)
: number2 = Convert.ToDouble(Me.value2.Text)
:
: End Function
:
: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
:
: GenerateNewNumber()
:
: End Sub
:
: End Class
:
:
:

Ok add GenerateNewNumber() to the if statement, so it runs whenever a correct answer is found. At the moment it only runs when the form loads.

Hope that helps
-Fallen

• Thanks for the help. That and a few other changes has it working perfectly! Have a great day,

Linda
• : Thanks for the help. That and a few other changes has it working perfectly! Have a great day,
:
: Linda
:

Welcome • : Hello....again! I almost have the below program working correctly, but not quite...It is suppose to display the mult. problem and then the user enters their answer and clicks the button. If the answer is correct, it displays a good job message and displays a new problem. If they are wrong, it says try again and the problem remains the same until it is answered correctly. I have it now so that it recognizes the difference between right and wrong answers, but it will not generate a new problem. I am sure there is something wrong with the loops somewhere, but I can't find the mistake!!!! Thanks for any help you can give me! Linda
:
: Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
:
: Dim number1, number2, userGuess As Integer
: Dim i As String
:
: number1 = value1.Text
: number2 = value2.Text
: userGuess = InputBox.Text
:
: 'call to check user's answer
: CheckCalculation(number1, number2, userGuess)
:
: If i = 2 Then
: GenerateNewNumber()
: Else
: CheckCalculation(number1, number2, userGuess)
: End If
:
: End Sub
:
: Function CheckCalculation(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As String
:
: Dim reply As String = "That is right, great job! Clear the box and try the next one!"
: Dim reply1 As String = "Sorry, please clear the box and try again!"
: Dim returned As Integer
:
: If z <> x * y Then
: Else
: Return 2
: End If
:
:
: End Function
:
: Function GenerateNewNumber()
:
: Dim randomObject As Random = New Random()
: Dim randomNumber As Integer = randomObject.Next()
:
: 'two random numbers being multiplied
: Dim number1 As Integer
: Dim number2 As Integer
:
: Dim userGuess As Integer
:
: 'random numbers between 0 and 9 are generated
: number1 = randomObject.Next(0, 10)
: number2 = randomObject.Next(0, 10)
:
: 'assigns appropriate variable to textboxes for display
: Me.value1.Text = String.Format("{0}", number1)
: Me.value2.Text = String.Format("{0}", number2)
:
: 'converts numbers to be mult. to integers
: number1 = Convert.ToDouble(Me.value1.Text)
: number2 = Convert.ToDouble(Me.value2.Text)
:
: End Function
:
: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
:
: GenerateNewNumber()
:
: End Sub
:
: End Class
:
:
:

Linda, I missed this on the earlier iterations, but there is something else to look at. You are calling CheckCalculation as a function rather than a subroutine. Normally you would do that because you want to get a return value and use it for something. In this case you return 2 if the answer was correct. I would think you want to use that in the calling program to determine whether to give a new problem or look for a better answer from your user. In fact, you are ignoring it. You are testing on a string value i that never even gets initialized (other than by the system). You can correct this by using the the result:

Don't say: if i = 2 Then

Say:

if CheckCalculation(number1, number2, userGuess) = 2 Then

When they answer correctly in your CheckCalculation function, CheckCalculation becomes = to 2 (When you say 'Return 2'). If it were me I would also return a different value for a wrong answer just to control the function value. If I wasn't going to use the value returned I would use a subroutine rather than a function.

The suggestion to add the call to GenerateNewNumber in the If statement certainly works, but it is a workaround to the real problem.