Results 1 to 3 of 3

Thread: [RESOLVED] Calculator Addition Not Working

  1. #1

    Thread Starter
    New Member ellipsis95's Avatar
    Join Date
    Jun 2008
    Posts
    12

    Resolved [RESOLVED] Calculator Addition Not Working

    Hey guys, I'm pretty bored and figured I'de make a calculator for my homework and everything. Well I've got subtraction, division, multiplication , etc working, but when I do add it adds the numbers togethor

    so, If I add 5 + 4 it equals 54.
    If I add 7+2 it equals 72. Everything else works fine. Can anyone tell me what I'm doing wrong?

    Code:
    If addbutton Then
    equalbox.Text = onebox.Text + twobox.Text
    End If

  2. #2

    Thread Starter
    New Member ellipsis95's Avatar
    Join Date
    Jun 2008
    Posts
    12

    Re: Calculator Addition Not Working

    ::EDIT::

    I'm sorry , You can easily fix the problem by doing the following.
    Code:
    Dim first As Double
    Dim second As Double
    first = onebox.Text
    second = twobox.Text
    If addbutton.Checked Then
    equalbox.Text = first + second
    End If

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] Calculator Addition Not Working

    The + operator is overloaded... when one of the operands is a string concatenation is performed instead of addition. Post #2 code relies on implicit conversion for storage into double variables. Explicit conversion is performed as follows:

    equalbox.Text = CDbl(onebox.Text) + CDbl(twobox.Text)

    You will also need to place text validation and/or error handling, otherwise conversion will throw an error if value is non-numeric (or cannot be converted to a numeric value).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width