Results 1 to 14 of 14

Thread: Problem With Square Root and Quadratic Formula

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    6

    Exclamation Problem With Square Root and Quadratic Formula

    I basically set up my program as is and it does not run correctly. Right now I am recieiving a "Run Time Error '13': Type mismatch". I don't have the MSDN library because I didn't install it when I first bought the program. If any of you could give some input, that would be great. I have made a few alternations but here is the way I currently have it set up.

    Dim X1 As String
    Dim X2 As String
    Dim a As String
    Dim b As String
    Dim c As String


    Private Sub cmdCalculate_Click()
    txtValueA = a
    txtValueB = b
    txtValueC = c
    If X1 = (-b + Sqr(b ^ 2 - 4 * a * c)) / 2 * a Then
    lblFirst.Caption = X1
    End If
    If X2 = (-b - Sqr(b ^ 2 - 4 * a * c)) / 2 * a Then
    lblSecond.Caption = X2
    End If
    End Sub

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Problem With Square Root and Quadratic Formula

    First of all, why are you using STRINGS to calculate? Use a numeric type!
    Quote Originally Posted by Jayj2k1
    Dim X1 As String
    Dim X2 As String
    Dim a As String
    Dim b As String
    Dim c As String
    and furthermore,
    what are you calculating??
    VB Code:
    1. Private Sub cmdCalculate_Click()
    2. txtValueA = a 'where do you define a??
    3. txtValueB = b 'where do you define b??
    4. txtValueC = c 'where do you define c??
    5. 'What is X1? You only use it if it fits the formula?
    6. If X1 = (-b + Sqr(b ^ 2 - 4 * a * c)) / 2 * a Then
    7. lblFirst.Caption = X1
    8. End If
    9. 'What is X2? You only use it if it fits the formula?
    10. If X2 = (-b - Sqr(b ^ 2 - 4 * a * c)) / 2 * a Then
    11. lblSecond.Caption = X2
    12. End If
    13. End Sub
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    6

    Re: Problem With Square Root and Quadratic Formula

    Forget the String type. I'm not sure why I did that but instead I am now using double. Basically, I have set up a program that performs that allows a quadratic equation (Ex. Ax2 + Bx + C= 0), to be calculated with the quadratic formula. I made an attachment so that you can see the formula that I am trying to put into Visual Basic Code.

    In my program, I created 3 text boxes where the end user will enter a value for A, B, and C (all numeric). Once they do this, they will click the Calculate command button where the program will use the formula. I have been getting a "Run Time Error '6': Overflow" as well. And by the way, how do I define A, B, and C. Once you look at the formula that I attached with the post, you should have a better idea of what I am trying to accomplish here. Thanks for your help and patience.
    Attached Images Attached Images  

  4. #4
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Problem With Square Root and Quadratic Formula

    Er..when you set A=B, this means that you are setting A to be equal to what is already in B.

    Hence you need your definitions of a,b,c to be the other way around.

    Also, you don't need "If...Then", you aren't checking a condition. You just need to set X1=... X2=...
    Then stick them in the labels.

    zaza

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    6

    Re: Problem With Square Root and Quadratic Formula

    So if you were to use three text boxes for the end user to enter A, B, and C and then use that formula that I have a picture of, how exactly would you code it to make it work all together?

  6. #6
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Problem With Square Root and Quadratic Formula

    Hence you need your definitions of a,b,c to be the other way around.

    Also, you don't need "If...Then", you aren't checking a condition. You just need to set X1=... X2=...
    Then stick them in the labels.

    zaza

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    6

    Re: Problem With Square Root and Quadratic Formula

    Here is what my program looks like now. Basically you are telling me to just enter in the value of X1 and X2 and I'm fine. However, what is the proper way to declare these values because X1 and X2 will be displayed in the two label boxes next to the X?
    Attached Images Attached Images  

  8. #8
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Problem With Square Root and Quadratic Formula

    To display a numeric value in a textbox use:
    VB Code:
    1. Textbox.text=cstr(NumericValue)

    To calculate your X1
    VB Code:
    1. a=csng(textboxA.text)
    2. b=csng(textboxB.text)
    3. c=csng(textboxC.text)
    4.  
    5. X1=(-b + Sqr(b ^ 2 - 4 * a * c)) / 2 * a

    You should be able to do the rest!!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    6

    Re: Problem With Square Root and Quadratic Formula

    Ok. The new code that I have in now runs through ok. I get errors at times depending on the numbers I put in for A, B, and C.

    One thing I did notice is that my answers are way off. When I substituted 5 for A, 8 for B, and 2 for C, I came up with -7.75 for X1 and like -32.37 for X2.

    I know that those answers are way off because normally your answer should be a difference of maybe positive 1 or 2. Nothing more than that. Once again, I will post my code as well as how the program looked when I ran it.


    Option Explicit
    Dim X1 As Double
    Dim X2 As Double
    Dim a As Double
    Dim b As Double
    Dim c As Double

    Private Sub cmdCalculate_Click()
    a = CSng(txtValueA.Text)
    b = CSng(txtValueB.Text)
    c = CSng(txtValueC.Text)
    X1 = (-b + Sqr(b ^ 2 - (4 * a * c))) / 2 * a
    X2 = (-b - Sqr(b ^ 2 - (4 * a * c))) / 2 * a
    txtFirst.Text = CStr(X1)
    txtSecond.Text = CStr(X2)
    End Sub

    Private Sub cmdClear_Click()
    txtValueA = ""
    txtValueB = ""
    txtValueC = ""
    txtFirst = ""
    txtSecond = ""
    End Sub
    Attached Images Attached Images  

  10. #10
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Problem With Square Root and Quadratic Formula

    Your original formula had a mistake, use
    VB Code:
    1. X1 = (-b + Sqr(b ^ 2 - (4 * a * c))) / (2 * a)
    2. X2 = (-b - Sqr(b ^ 2 - (4 * a * c))) /( 2 * a)
    instead!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  11. #11
    Lively Member
    Join Date
    Jun 2005
    Posts
    76

    Re: Problem With Square Root and Quadratic Formula

    have you considered the case of a = 0?

    or when the solutions dont exist in R? eg the discriminant < 0?

  12. #12
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Problem With Square Root and Quadratic Formula

    And what if the user enters text in the boxes instead of numbers?

    That's where the nice if...then statements come in handy.

    The IsNumeric(number) function returns true if the 'number' is really a number or false if not.

    The other special cases, when b^2-4ac is less than or maybe equal to zero and when a = 0, can be handled using simple if statements. More questions would likely be handled best in the regular programming forum from here since the math part has been answered/dealt with. Of course, more discussion on the nature of the quadratic formula would qualify here


    Edit: PS, nice UI
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  13. #13
    Lively Member
    Join Date
    Jun 2005
    Posts
    76

    Re: Problem With Square Root and Quadratic Formula

    isnumeric will do the job but is a legacy function, correct?

    im curious to know what the best way of validating a text box for numeric data.

    currently i would use a regex, but is there a cleaner way to do it?

  14. #14
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Problem With Square Root and Quadratic Formula

    You can use val(text1.text), but it will evaluate as 0 if there isn't a number.

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