This code suppose to take the user's input of current balnace, the annual interest rate, the current month, and amount he/she will pay per month. When the CALCULATE button is clicked the program starting with the current month, should compute and display for each month until the balance is reduced to zero, the month, the interest charge, payment, and new balance. But somehow im stuck in an infinite loop and its not displaying anything. please help me
Code:
Public Class frmPaySchedule
Private Sub btncalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalculate.Click
Dim month As Integer = CInt(txtmonth.Text)
Dim balance As Decimal = CDec(txtbalance.Text)
Dim interest As Decimal = CDec(((txtAPR.Text / 100) / 12) * balance)
Dim payment As Decimal = CDec(txtpayment.Text)
Do Until balance = 0
If month = 5 Or month = 6 Or month = 7 Or month = 8 Then
payment = payment + 100
End If
If month = 11 Then
payment = payment + 50
End If
balance = balance - (payment - interest)
txtResult.Text = month.ToString & " " & FormatCurrency(interest) & " " & _
FormatCurrency(payment) & " " & FormatCurrency(balance)
month += 1
Loop
End Sub
End Class
Definitly Go with the break point stuff DG says as that is a good way to debug progams. But from what I can tell by just looking at it as that Your loop condition is Do something until balance = 0. So balance never equals zero so that's why you're in the endless loop. And I'm sure you knew that. The conditional stuff is doing it's thing. Its decementing values. But it never gives you the exact 0 you need to terminate the loop. Maybe what you want to say is if balance equals zero or if balance is less than zero. You probably got onto the negative side of the number line and since balance isn't zero it just keeps going.
The loop isn't the problem. You're always clobbering the textbox with your new value after every iteration. Try using a listview to get the nice organized look. Or try a the listbox control which is more simple but you won't get the nice grid look.
yea i changed it to a label box, but looking from the results the exit statement "do until balance = " cannot be 0, should it be set as a decimal or currency or something else for the loop to work, thats what im having trouble with.
There are more than one problem. First the payment is increasing during the loop and second the loop condition balance=0 is not being met.
and an "or balance < 0" to the loop condition to break out of the loop.
Also if it is not your intention to add 100 to your payment amount in 4 of the months and another 50 in month 11 then you need to reset the payment far within your loop as it is the payment would get pretty steep by the end.
If your starting payment was 50 then when you get to month 5 it would be 150 then 250 then 350 and so on. If this is the intent then it is fine but if not....
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.