Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2003
    Location
    California
    Posts
    231
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question vb adding problem

    can someone tell me whats wrong with this code?
    when i type two numbers to be added, it just puts the numbers together, like 12+12 = 1212. I dont understand whats wrong here:

    dim a, b, answer
    a = inputbox ("What is the first number? ")
    b = inputbox ("What is the second number? ")
    answer = a+b
    msgbox ("The sum is " & answer )

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The return value from InputBox is a string, so VB is just doing it's "string addition", which is concatenation.
    You need to cast the variables to Integer before you add them:
    Code:
    ' you should also be using the IsNumeric function before this to check
    ' that the values entered are actually numbers, otherwise the CInt() calls
    ' will give you a runtime error
    answer = CInt(a)+CInt(b)
    shmoove


  •  

    Posting Permissions

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