Calculator Operator Problems

BrandonMPSMI

Active member
Joined
Oct 8, 2008
Messages
43
Programming Experience
Beginner
Hey guys,

I'm new here but I do have some experience in program. Basically I'm learning VB for my job. I was asked to design a calculator. I've been doin' it from scratch and so far its working great. One problem. I've got the + button to work but all the others don't perform as expected. here's the code for the Plus button

VB.NET:
Private Sub Button_Addition_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Addition.Click
        'If there is nothing in the temporary storage for value 1
        If Total1 = 0 Then
            'Then change the temporary storage to what's on the screen.
            [COLOR="Blue"][U]Total1 = Total1 + Val(TxtDisplay.Text)[/U][/COLOR]
            inputStatus = True
            hasDecimal = False
        Else 'But if there is we want the txtdisplay to reflect it.
            [COLOR="Blue"][U]Total1 = Total1 + Val(TxtDisplay.Text[/U][/COLOR])
            tmpValue = Total1
            TxtDisplay.Text = Str(tmpValue)
            inputStatus = True
            hasDecimal = False
        End If
    End Sub

The part I highlighted is where it really all happens. So if I want to do say... Subtaction I just code this correct?

VB.NET:
Total1 = Total1 [B]-[/B] Val(TxtDisplay.Text)

The only problem is if I go 5 - 2 I get -3 (That's weird)

For multiplication I use

VB.NET:
Total1 = Total1 [B]*[/B] Val(TxtDisplay.Text)
But just don't get anything at all. (Same for division)

what am i doing wrong?

Also here's the code for the equal button.
VB.NET:
 Private Sub Button_Equal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Equal.Click
        Total2 = Total1 + Val(TxtDisplay.Text)
        TxtDisplay.Text = Total2
        Total1 = 0
    End Sub
 
Here's what is happening:

In the section of your code where you are initializing "Total1", you have Total1(0) = Total1(0) + <value>. This works for addition.

But with subtraction, you are saying Total1(0) = Total1(0) - <value> (aka: a negative number).

Just change this to Total1 = <value> and everything should work fine.:D
 
Back
Top