Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
My code for a program called CD Calculator keeps crashing; however, I do not know what is wrong with it.
VB
Public Class Form1
    Dim intInitialInvestment As Integer
    Dim decInterestRate As Decimal
    Dim intValue As Integer
    Dim intYears As Integer




    Private Sub btnCalculate_Click(sender As Object, e As System.EventArgs) Handles btnCalculate.Click

        intInitialInvestment = Val(Me.txtInvestment.Text)
        decInterestRate = Val(Me.txtRate.Text)
        decInterestRate = decInterestRate / 100
        intValue = Val(Me.txtValue.Text)

        Dim i As Integer = 1
        Do Until intInitialInvestment = intValue
            intInitialInvestment = intInitialInvestment + (intInitialInvestment * decInterestRate)
            intYears += i

            If intInitialInvestment = intValue Then
                Me.lblAnswer.Text = "The amount of years are: " & intYears.ToString
            End If

        Loop

        


    End Sub
End Class


[edit]Code block added, Tags changed - OriginalGriff[/edit]
Posted
Updated 15-Mar-15 6:51am
v2
Comments
OriginalGriff 15-Mar-15 12:52pm    
When you post a question, please take care to get the tags right: tagging your VB problem as C++ means that the majority of VB users don't even look at it!
[no name] 15-Mar-15 12:56pm    
Did you run it in the debugger? On which line does it "crash"?
Member 11526780 15-Mar-15 13:05pm    
I actually do not know where the issue is in; Whenever I run it it crashes and indicates that my accumulator is wrong or its overflowing.
Member 11526780 15-Mar-15 13:12pm    
Oops... Nevermind! I thinked I figured it out! Thank You!

1 solution

You don;t say what happens when it crashes, but I'd guess that the likely cause is that intInitialInvestment never equals the value of intValue, so your loop never exits.

Start by looking at what happens in the debugger: put a breakpoint on the line
VB
Dim i As Integer = 1
and run your program.
When you hit the breakpoint, the program will stop and let you control what is happening.
Step through, and look closely at what happens to intInitialInvestment each time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900