[RESOLVED] thanks! Program help? am i doin this right?? pleasee
The sum of the series 1/1 +1/2 +1/3.... never converges. Thus if you add enough terms you can reach any value/ For example it takes 4 terms of the series to reach 2 as 1 + 1/2 +1/3 +1/4 is 2.0833333 and 11 terms to reach 3.
AM i writing this program correctly???
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculate.Click
Dim thenumber As Integer
Dim theterms As Integer
Dim z As Integer
Dim n As Integer
Dim thesum As Decimal
thenumber = CInt(number.Text)
theterms = CInt(terms.Text)
thesum = CDec(sum.Text)
theterms = 1
thesum = 0
z = 1 / n
n = 1
Do While thesum < thenumber
thesum = z
z = z + z
n = n + 1
Loop
terms.Text = CStr(theterms)
sum.Text = CStr(thesum)
I keep getting an error messege.... please help i am desperate
Re: Program help? am i doin this right?? pleasee
Where are your errors?
One should be here:
you set n=1 AFTER that line, so n is 0 and therefore you have a division by Zero!
Re: Program help? am i doin this right?? pleasee
good call. I stil get the same error messege though... Conversion from "" to type "integer" is not valid
Re: Program help? am i doin this right?? pleasee
Where does this error occur?, it should be highlighted in yellow (at least in VB6, but I guess you use VB.Net).
Re: Program help? am i doin this right?? pleasee
im using vb 2005 express edition. no highlighting
Re: Program help? am i doin this right?? pleasee
Stop making duplicate threads.
Conversion maybe invalid if you have decimals on your textboxes? You try to convert it to an integer make sure it's really an integer.
Try CDbl and change your declaration to Dim Double.
Re: Program help? am i doin this right?? pleasee
Try this:
VB Code:
theterms = CInt(terms.Text)
'theterms.text holds the maximum term (i.e. divisor) to be used!
thesum = 0
n = 1
z = 1 / n
Do While n < theterms
z = 1/(n + 1)
thesum = z+thesum
n=n+1
Loop
sum.Text = CStr(thesum)
Re: Program help? am i doin this right?? pleasee
ugghh I stil tried that and the error mesege popped up again about conversion
Re: Program help? am i doin this right?? pleasee
Try this.
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculate.Click
Dim thenumber As Double, theterms As Double, z As Double, n As Double, thesum As Double
thenumber = CDbl(number.Text)
theterms = CDbl(terms.Text)
thesum = CDbl(sum.Text)
theterms = 1
thesum = 0
n = 1
z = 1 / n
Do While thesum < thenumber
thesum = z
z = z + z
n = n + 1
Loop
terms.Text = CStr(theterms)
sum.Text = CStr(thesum)
Re: Program help? am i doin this right?? pleasee
now i get an error:
Conversion from string "" to type "double" is not valid
Re: Program help? am i doin this right?? pleasee
Weird. Try to change the last code to.
VB Code:
terms.Text = theterms
sum.Text = thesum
Re: Program help? am i doin this right?? pleasee
It sounds like you have empty textboxes (sum.text, terms.text or number.text) when hitting the Button1! Try to catch that one or simply make a valid input!
Re: Program help? am i doin this right?? pleasee
Sorry a had a typo in the code posted :blush:
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculate.Click
Dim thenumber As Double
Dim theterms As Double
Dim z As Double
Dim n As Double
Dim thesum As Double
theterms = CInt(terms.Text)
'theterms.text holds the maximum term (i.e. divisor) to be used!
thesum = 0
n = 1
Do While n <= theterms
z = 1/(n)
thesum = z+thesum
n=n+1
Loop
sum.Text = CStr(thesum)
End Sub
Re: Program help? am i doin this right?? pleasee
Ok now im confused.
Change
VB Code:
thenumber = CDbl(number.Text)
theterms = CDbl(terms.Text)
thesum = CDbl(sum.Text)
To
VB Code:
thenumber = Val(number.Text)
theterms = Val(terms.Text)
thesum = Val(sum.Text)
Re: Program help? am i doin this right?? pleasee
Quote:
Originally Posted by zynder
Ok now im confused.
What did confuse you?
Re: Program help? am i doin this right?? pleasee
Refer to post #10
It says Conversion from string "" to type "double" is not valid
Maybe the textbox has no value or has characters on it. There is no error on the code by the way.
Re: Program help? am i doin this right?? pleasee
I still believe he gets this error, because he'S not checking wether the TextBox holds anything.
And for the code, I agree on "no errors" but does the code you copied from cthompson86 do the required job? I don't think so!
BTW cthompson86 seems to be gone, has he lost interest!
Re: Program help? am i doin this right?? pleasee
thank you very much I really appreciate it. I figured it out after i put val
-Chris
Re: [RESOLVED] thanks! Program help? am i doin this right?? pleasee
For what it's worth, your in the wrong Forum - you need to be in .Net. That said,
Val() is a hangover (in .Net) and should be used with caution - as you have found out, it can give wanted results, without the proper casting!