0

Hi i am just new to VB.NET and our teacher told us to experiment on for loop in a ODD SUM Program you need to add all odd numbers from Input One to Input Two like that but i can't stop it from adding continuously for example i input the first number 1 and the second 10 if i click the button one time the output should be 25 but when i click again the button it becomes 50 its wrong i can't terminate the process help me

Dim oddOne As Integer
        Dim oddTwo As Integer


     oddTwo = Val(txtInputTwo.Text)

        If txtInputOne.Text = "" And txtInputTwo.Text = "" Then
            MessageBox.Show("No Input, Please Input in Both Textboxes!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
        Else
            If txtInputOne.Text = "" Then
                MessageBox.Show("No Input, Please Input in TextBox 1", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            Else
                If txtInputTwo.Text = "" Then
                    MessageBox.Show("No Input, Please Input in TextBox 2", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                Else
                    If IsNumeric(txtInputOne.Text) Or IsNumeric(txtInputTwo.Text) Then
                        If Val(txtInputOne.Text) Then
                            If Val(txtInputTwo.Text) Then
                                If Val(txtInputOne.Text) > Val(txtInputTwo.Text) Then
                                    MessageBox.Show("Your First Number Must be Greater Than the Second Number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                                Else
                                    For oddOne = Val(txtInputOne.Text) To oddTwo

                                        If oddOne Mod 2 = 1 Then
                                            If oddOne < oddTwo Then
                                                lblOutput.Text = Val(lblOutput.Text) + oddOne
                                                oddOne += 1
                                            End If
                                        End If


                                    Next
                                End If
                            Else
                                MessageBox.Show("Wrong Input in TextBox2, Must be a Number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                            End If
                        Else
                            MessageBox.Show("Wrong Input in TextBox 1, Must be a Number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                        End If
                    Else
                        MessageBox.Show("Both Input are Incorrect Must Enter A Number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                    End If
                End If
            End If
        End If

Edited by vijaybrar: n/a

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by adam_k
0

Next time start with

lblOutput.Text = 0

this will clear the result from the previous calculation.
If the first time you've run your sub you got the result correct, then this should be your only problem.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.