I am making a calculator in Virtual Basic for almost a week. I am encountering a problem when using the equals sign. When I type problems like 4 + 6 = 10 it answers correctly. When I press the equal again immediately after the calculator answers 10. It should enter as 10 + 6 = 16 but it does not work like that. How do I create a calculator that when users press again and again they will have a loop of + 6? Please help me. This is the code I used for my equal sign button.

Recommended Answers

All 6 Replies

This the codes I used for my equal sign button.

Private Sub btneqClick(sender As Object, e As EventArgs) Handles btneq.Click
        num2 = Convert.ToDouble(Display.Text)

        If op = "+" Then
            ans = num1 + num2
            Display.Text = Convert.ToString(ans)

        ElseIf op = "-" Then
            ans = num1 - num2
            Display.Text = Convert.ToString(ans)

        ElseIf op = "÷" Then
            ans = num1 / num2
            Display.Text = Convert.ToString(ans)

        ElseIf op = "×" Then
            ans = num1 * num2
            Display.Text = Convert.ToString(ans)

    End Sub

You have to adjust the value for num1 so that the next time you click = you get the correct answer. If you want ans to be the sum of num1 and num2 the first time, then num2 and ans the next time, you'll have to assign ans to num1. That way each time you click = you will add another num2 to the result.

commented: I understand what you mean but I do not know the code of that. I am sorry can you type it in Visual Basic Language? +0

As a further note, if you see the same line of code in every if-else block, that's a good sign you should move it out of the block and put it just once at the end. You also might want to look into the select statement. It's a little cleaner.

commented: I already done this one but I cannot code the one you mentioned on the other comment +0

This is the updated version of the codes I used

    Private Sub btneqClick(sender As Object, e As EventArgs) Handles btneq.Click
        num2 = Convert.ToDouble(Display.Text)

        Select Case op
            Case "+"
                ans = num1 + num2
                Display.Text = Convert.ToString(ans)
            Case "-"
                ans = num1 - num2
                Display.Text = Convert.ToString(ans)
            Case "÷"
                ans = num1 / num2
                Display.Text = Convert.ToString(ans)
            Case "×"
                ans = num1 * num2
                Display.Text = Convert.ToString(ans)
        End Select

    End Sub

I edited the code again because I just remembered that you cannot divide anything by 0

Private Sub btneqClick(sender As Object, e As EventArgs) Handles btneq.Click
        num2 = Convert.ToDouble(Display.Text)

        Select Case op
            Case "+"
                ans = num1 + num2
                Display.Text = Convert.ToString(ans)
            Case "-"
                ans = num1 - num2
                Display.Text = Convert.ToString(ans)
            Case "÷"
                ans = num1 / num2
                Display.Text = Convert.ToString(ans)
                If num2 = 0 Then
                    Display.Text = "error!"
                End If

            Case "×"
                ans = num1 * num2
                Display.Text = Convert.ToString(ans)
        End Select

    End Sub

The following may, or may not work depending on the scope of num1.

Private Sub btneqClick(sender As Object, e As EventArgs) Handles btneq.Click

    num2 = Convert.ToDouble(Display.Text)

    Select Case op
        Case "+"
            ans = num1 + num2
            num1 = ans
            Display.Text = Convert.ToString(ans)
        Case "-"
            ans = num1 - num2
            Display.Text = Convert.ToString(ans)
        Case "÷"
            ans = num1 / num2
            Display.Text = Convert.ToString(ans)
            If num2 = 0 Then
                Display.Text = "error!"
            End If

        Case "×"
            ans = num1 * num2
            Display.Text = Convert.ToString(ans)
    End Select

End Sub
commented: what should be the scope of num1 for this to work? hahahaha it did not work on my code +0
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.