CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2014
    Posts
    3

    Help on Compound Interest Program?

    Hello,
    I am working on a VB program that calculates the length of time it will take for an investment to double in a savings account. It runs but unfortunately only returns 0. Could someone give me some insight where to go from here? I am still a student and very much a novice, so please bear with me!


    c6_235_30.zip

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Help on Compound Interest Program?

    Moved from VB6 to VB.Net Code is using VB.Net 2012 not VB6 This is the section of related code from the zip file
    Code:
      Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click
    'declare variables         
    Dim Amount As Double = CDbl(txtAmount.Text)         
    Dim Rate As Decimal = CDbl(txtRate.Text)         
    Dim balance As Single         
    Dim numYears As Integer          
    Do While balance < (2 * balance) 
                balance = (1 + Rate) * balance
                 numYears += 1
             Loop          
    'display results         
    txtYears.Text = numYears    
     End Sub
    Given that balance is not assigned a value before the loop the code inside the loop will never execute.
    If balance did have a value then you would have a new problem in that it would become an endless loop since Balance will always be less than Balance * 2
    The only reason you do not get an endless loop now is because balance=0 and 0*2 is not less than 0

    You should probably be using Amount as the last var on the Do statement rather than balance.
    Last edited by DataMiser; September 7th, 2014 at 07:21 PM.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Sep 2014
    Posts
    3

    Re: Help on Compound Interest Program?

    Thank you! So removing the balance variable entirely and replacing amount into the Do While will fix my problem?

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Help on Compound Interest Program?

    No, don't remove the balance var entirely just the second instance of it on that line.
    You want the loop to go until Balance is double the starting amount to do this you must use both balance and amount for the test
    Always use [code][/code] tags when posting code.

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

Featured