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
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.
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?
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?
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
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