# Thread: HELP ASAP!! Infinite Loop? (VB.NET)

1. ## HELP ASAP!! Infinite Loop? (VB.NET)

I am writing a program that I think I am getting an infinite loop on. Can't seem to figure out where. I am sure it is more than likely the first loop being done. Anyway so you know what I am trying to do. I am asking for someone to enter an amount they are "depositing" into an account that is compiled annually by an interest also provided by the user. From that information I am to determine how long it will take for that initial amount of money to double and how long it is to take to make a million dollars. Every time I run the program it freezes up on me any help provided is much appreciated. Here is my code.

<pre> Private Sub btnDetermine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetermine.Click

'Declare Variables
Dim balance, r As Double
Dim numYearsMil As Integer
Dim numYearsDoub As Integer

'Get initial value of balance, and rate from a textbox
balance = CDbl(txtAmount.Text)
r = CDbl(txtRate.Text)

'Loop determines the number of years it will take to double your money
Do While balance <= (balance * 2)

balance += r * balance
numYearsDoub += 1

Loop 'Until balance >= (balance * 2)

'Prints the number of years it takes to double your money
txtDouble.Text = "in " & numYearsDoub & " years you will double your money."

'Loop determins the number of years it will take to become a millionare
Do While balance < 1000000

balance += r * balance
numYearsMil += 1

Loop

'Prints the number of years it takes to become a millionare
txtMillion.Text = "In " & numYearsMil & " years you will have a million dollars."

End Sub</pre>

2. ## Re: HELP ASAP!! Infinite Loop? (VB.NET)

Ok I've narrowed down the problem. It is in the first Do Loop. For some reason the doubleing money is holding up the system. Without that I can easily get the number of years it takes to make a million. But if that loop is there then it bogs up the system and gives me odd numbers. I don't know what to do with it. Oh I took out the = sign in the first loop and it stoped freezing on me.

3. ## Re: HELP ASAP!! Infinite Loop? (VB.NET)

Narrowed the problem to one line. Some where in the Do Loop statement it is messing up. WHen I get the number calc it is saying something like 11984 for the number of years to double. Ok so then if I have the loop statement like this

Do While balance < (balance * 2)
balance = (r + 1) * balance
numYearsDoub += 1
Loop

where is it wrong? I know it is the Do While balance < (balance * 2) but I can't figure out why that is the problem. Any help is appreciated.

Here is updated code.

<pre>Private Sub btnDetermine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetermine.Click

'Declare Variables
Dim balance, r, amount As Double
Dim numYearsMil, numYearsDoub As Integer

'Get initial value of balance, and rate from a textbox
balance = CDbl(txtAmount.Text)
amount = CDbl(txtAmount.Text)
r = CDbl(txtRate.Text)

Do While amount < amount * 2

amount = (1 + r) * amount
numYearsDoub += 1

Loop

'Loop determines the number of years it will take to double your money
Do While balance < 1000000

balance = (1 + r) * balance
numYearsMil += 1

Loop

'Prints the number of years it takes to become a millionare
txtDouble.Text = "In " & numYearsDoub & " years you will double your money."
txtMillion.Text = "In " & numYearsMil & " years you will have a million dollars."

End Sub</pre>

4. ## Re: HELP ASAP!! Infinite Loop? (VB.NET)

Won't x always be less than x * 2 ? Until you overflow the maximum size of a "double"... I think you want the comparison on the right to refer to something like CDbl(txtAmount.Text) * 2 rather than using the variable you are changing inside the loop.

5. ## Re: HELP ASAP!! Infinite Loop? (VB.NET)

You could set up a variable like dblBaseAmount = CDbl(txtAmount.Text) then set amount and balance = to it and use do while amount < BaseAmount * 2 in the first loop

6. ## Re: HELP ASAP!! Infinite Loop? (VB.NET)

Thanks for the tips I ended up figuring it out. When I get home I will post the code if needed.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•