|
-
Feb 16th, 2007, 01:07 AM
#1
Thread Starter
Junior Member
[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
Last edited by cthompson86; Feb 16th, 2007 at 07:18 AM.
-
Feb 16th, 2007, 01:16 AM
#2
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!
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!
-
Feb 16th, 2007, 01:19 AM
#3
Thread Starter
Junior Member
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
-
Feb 16th, 2007, 01:24 AM
#4
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).
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!
-
Feb 16th, 2007, 01:26 AM
#5
Thread Starter
Junior Member
Re: Program help? am i doin this right?? pleasee
im using vb 2005 express edition. no highlighting
-
Feb 16th, 2007, 01:30 AM
#6
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.
-
Feb 16th, 2007, 01:30 AM
#7
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)
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!
-
Feb 16th, 2007, 01:44 AM
#8
Thread Starter
Junior Member
Re: Program help? am i doin this right?? pleasee
ugghh I stil tried that and the error mesege popped up again about conversion
-
Feb 16th, 2007, 01:47 AM
#9
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)
-
Feb 16th, 2007, 01:51 AM
#10
Thread Starter
Junior Member
Re: Program help? am i doin this right?? pleasee
now i get an error:
Conversion from string "" to type "double" is not valid
-
Feb 16th, 2007, 02:03 AM
#11
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
-
Feb 16th, 2007, 02:14 AM
#12
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!
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!
-
Feb 16th, 2007, 02:31 AM
#13
Re: Program help? am i doin this right?? pleasee
Sorry a had a typo in the code posted
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
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!
-
Feb 16th, 2007, 02:49 AM
#14
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)
-
Feb 16th, 2007, 02:52 AM
#15
Re: Program help? am i doin this right?? pleasee
 Originally Posted by zynder
Ok now im confused.
What did confuse you?
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!
-
Feb 16th, 2007, 02:56 AM
#16
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.
-
Feb 16th, 2007, 03:00 AM
#17
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!
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!
-
Feb 16th, 2007, 07:16 AM
#18
Thread Starter
Junior Member
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
-
Feb 16th, 2007, 07:21 AM
#19
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|