Results 1 to 19 of 19

Thread: [RESOLVED] thanks! Program help? am i doin this right?? pleasee

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    Resolved [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.

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

    Re: Program help? am i doin this right?? pleasee

    Where are your errors?

    One should be here:
    VB Code:
    1. z = 1 / n
    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!

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    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

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

    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!

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    Re: Program help? am i doin this right?? pleasee

    im using vb 2005 express edition. no highlighting

  6. #6
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    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.

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

    Re: Program help? am i doin this right?? pleasee

    Try this:
    VB Code:
    1. theterms = CInt(terms.Text)
    2. 'theterms.text holds the maximum term (i.e. divisor) to be used!
    3.  
    4. thesum = 0
    5.  
    6. n = 1
    7. z = 1 / n
    8.  
    9.  
    10. Do While n < theterms
    11.  
    12.  
    13. z = 1/(n + 1)
    14. thesum = z+thesum
    15. n=n+1
    16. Loop
    17.  
    18. 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!

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    Re: Program help? am i doin this right?? pleasee

    ugghh I stil tried that and the error mesege popped up again about conversion

  9. #9
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Program help? am i doin this right?? pleasee

    Try this.

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculate.Click
    2.  
    3. Dim thenumber As Double, theterms As Double, z As Double, n As Double, thesum As Double
    4.  
    5. thenumber = CDbl(number.Text)
    6. theterms = CDbl(terms.Text)
    7. thesum = CDbl(sum.Text)
    8.  
    9. theterms = 1
    10. thesum = 0
    11. n = 1
    12. z = 1 / n
    13.  
    14.  
    15. Do While thesum < thenumber
    16. thesum = z
    17. z = z + z
    18. n = n + 1
    19.  
    20. Loop
    21.  
    22. terms.Text = CStr(theterms)
    23. sum.Text = CStr(thesum)

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    Re: Program help? am i doin this right?? pleasee

    now i get an error:

    Conversion from string "" to type "double" is not valid

  11. #11
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Program help? am i doin this right?? pleasee

    Weird. Try to change the last code to.

    VB Code:
    1. terms.Text = theterms
    2. sum.Text = thesum

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

    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!

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

    Re: Program help? am i doin this right?? pleasee

    Sorry a had a typo in the code posted
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculate.Click
    2.  
    3. Dim thenumber As Double
    4. Dim theterms As Double
    5. Dim z As Double
    6. Dim n As Double
    7. Dim thesum As Double
    8.  
    9. theterms = CInt(terms.Text)
    10. 'theterms.text holds the maximum term (i.e. divisor) to be used!
    11.  
    12. thesum = 0
    13. n = 1
    14. Do While n <= theterms
    15.   z = 1/(n)
    16.   thesum = z+thesum
    17.   n=n+1
    18. Loop
    19.  
    20. sum.Text = CStr(thesum)
    21.  
    22. 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!

  14. #14
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Program help? am i doin this right?? pleasee

    Ok now im confused.

    Change

    VB Code:
    1. thenumber = CDbl(number.Text)
    2. theterms = CDbl(terms.Text)
    3. thesum = CDbl(sum.Text)

    To

    VB Code:
    1. thenumber = Val(number.Text)
    2. theterms = Val(terms.Text)
    3. thesum = Val(sum.Text)

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

    Re: Program help? am i doin this right?? pleasee

    Quote 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!

  16. #16
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    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.

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

    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!

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    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

  19. #19
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    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
  •  



Click Here to Expand Forum to Full Width