CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Nov 2015
    Posts
    15

    A little help please...

    Hey, im new here and i need a little help with a program from school...


    4. A certificate of deposit (CD) allows you to invest an amount of money at a specific interest rate for a period of time. Create a CD Calculator application that accepts and initial investment amount, the annual interest rate, and the desired ending value in input boxes and then displays the number of years it will take for the CD to be worth the specified ending value when interest is compounded annually. The CD value at the end of each year can be calculated by the formula CD Value = CD Value + (CD Value * Interest Rate). To determine the number of years it will take for the CD to reach the desired ending value, repeatedly execute the formula until the CD value is equal to or greater than the desired ending value. The application interface should look similar too:

    The forum consist of three text boxes where the user can enter the CD value, percent of interest, and ending value. It also has a button for calculation. And a label to display the amount of years it will take for the initial investment to be equal to or greater than the ending value.

    I am unsure how to code such a program. Any help will be appreciated.

    Thank You!

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

    Re: A little help please...

    It is pretty simple.
    Use a loop, within the loop do the math, repeat until the value is the desired amount. Don't forget to keep track of how many times the loop executes as it works toward the desired value.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Nov 2015
    Posts
    15

    Re: A little help please...

    Here is the code I have so far, but it gives me a run-time error; it just freezes...could you explain why...

    Public Class frmtittle

    Private Sub btncalculation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalculation.Click
    Dim investment As Integer = Val(Me.txtinitalinvestmentinput.Text)
    Dim interest As Integer = Val(Me.txtannualinterestrateinput.Text)
    Dim ending As Integer = Val(Me.txtendingvalueinput.Text)
    Dim counter As Integer = 0
    Dim current As Integer = 0

    interest = interest * 0.01

    Do While current < ending
    investment = investment + (investment * interest)
    counter += 1
    Loop

    Me.lblresult.Text = counter

    End Sub
    End Class

    Thanks

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

    Re: A little help please...

    Well current is 0 and never changes so it will always be <ending and the loop will never end.
    You need to update the value of current in your loop
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Nov 2015
    Posts
    15

    Re: A little help please...

    How do you update the value?

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

    Re: A little help please...

    by using an = assignment.

    In this case I assume you want current to be equal to investment or better still just get rid of current completely and use investment as that is what you are updating in the loop
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Nov 2015
    Posts
    15

    Re: A little help please...

    Ok so I got rid of current and replaced it with investment but it is still freezing. Here is what the code looks like know:

    Public Class frmtittle

    Private Sub btncalculation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalculation.Click
    Dim investment As Integer = Val(Me.txtinitalinvestmentinput.Text)
    Dim interest As Integer = Val(Me.txtannualinterestrateinput.Text)
    Dim ending As Integer = Val(Me.txtendingvalueinput.Text)
    Dim counter As Integer = 0

    interest = interest * 0.01

    Do While investment < ending
    investment = investment + (investment * interest)
    counter += 1
    Loop

    Me.lblresult.Text = counter

    End Sub
    End Class

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

    Re: A little help please...

    Well assuming that investment, interest and ending are all valid numeric values the loop should exit after a number of loops. How many loops will depend on those values of course and if the values are such that the loop has to run a bunch of times then you may get a not responding status even though the program would be working. This will happen in long running loops if steps are not taken to prevent it. Of course if the values are within reason the loop should be very quick.

    What values are you entering into the text boxes?
    Always use [code][/code] tags when posting code.

  9. #9
    Join Date
    Nov 2015
    Posts
    15

    Re: A little help please...

    Ok so i am inputing 2000 as the investment amount, 8 for the interest rate, and 5000 as the ending value. Yet the application just freezes...any toughts on what im doing wrong?

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

    Re: A little help please...

    Your variable types are incorrect for what you are doing.

    Integer holds a whole number so my guess would be that you are never getting any increment due to the values being rounded down. In effect what you are doing is adding 0 to investment each time and expecting it to grow.

    Your main variables should be of type Decimal here. Integer is ok for counter but not for the others

    Also note that the code you have posted is not VB6 code. It is VB.Net. That code would not run at all in VB6.

    I am moving the thread to the VB.Net section.
    Last edited by DataMiser; November 8th, 2015 at 08:44 AM.
    Always use [code][/code] tags when posting code.

  11. #11
    Join Date
    Nov 2015
    Posts
    15

    Re: A little help please...

    Thank you so much!! It finally worked, all I had to do was change the variables to decimal :P I am sorry about posting in the wrong section. If you don't mind could you help me with another problem. I am making a bowling scores application, that prompts the user to enter as many bowling scores as desired and then display the high score and the low score. The form consists of 1 label and two buttons, one being the button called enter scores and the other called statisitics.

    I have not actually written any code for this one, because I am unsure of the logic. What is the point of this application. Could you maybe provide some sample code?

    Thanks Again,
    Much Appreciated.

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