What's new
Fantasy Football - Footballguys Forums

Welcome to Our Forums. Once you've registered and logged in, you're primed to talk football, among other topics, with the sharpest and most experienced fantasy players on the internet.

Any VB programmers? (1 Viewer)

Tecumseh

Footballguy
Can you tell me why I am stuck in an infinite loop in these two programs?

Program 1- Trying to output the number of years required to double your principle with compound interest:

Code:
Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click        Dim amount, interest, years, newbalance, total As Double        amount = CDbl(txtAmount.Text)        interest = CDbl(txtInterest.Text)        Do While total <> (2 * amount)             newbalance = (amount * interest)            total = (newbalance * interest)            years += 1        Loop        txtYears.Text = CStr(years)     End Sub
 
Program 2- Trying to output the year that the world population will reach 10 billion

ETA- Solved!

Code:
Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click        Dim pop As Double = 7000000000, newpop, numyears As Double        Do While newpop < 10000000000            newpop = (pop * 0.0114)            numyears = 2011 + 1        Loop        txtOutput.Text = "World population will reach 10 Billion in " & numyears & "."    End Sub
 
Last edited by a moderator:
Program 2- Trying to output the year that the world population will reach 10 billion

Code:
Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click        Dim pop As Double = 7000000000, newpop, numyears As Double        Do While newpop < 10000000000            newpop = (pop * 0.0114)            numyears = 2011 + 1        Loop        txtOutput.Text = "World population will reach 10 Billion in " & numyears & "."    End Sub
Been a while but seems like pop never changes so newpop never changes.
 
Program 2- Trying to output the year that the world population will reach 10 billion

Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click Dim pop As Double = 7000000000, newpop, numyears As Double Do While newpop < 10000000000 newpop = (pop * 0.0114) numyears = 2011 + 1 Loop txtOutput.Text = "World population will reach 10 Billion in " & numyears & "." End Sub
Is this saying population increases by .0114 % a year? If so change 0.0114 to 1.0114 and you should be good I think.

 
Program 2- Trying to output the year that the world population will reach 10 billion

Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click Dim pop As Double = 7000000000, newpop, numyears As Double Do While newpop < 10000000000 newpop = (pop * 0.0114) numyears = 2011 + 1 Loop txtOutput.Text = "World population will reach 10 Billion in " & numyears & "." End Sub
Been a while but seems like pop never changes so newpop never changes.
That too.

 
Program 2- Trying to output the year that the world population will reach 10 billion

Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click Dim pop As Double = 7000000000, newpop, numyears As Double Do While newpop < 10000000000 newpop = (pop * 0.0114) numyears = 2011 + 1 Loop txtOutput.Text = "World population will reach 10 Billion in " & numyears & "." End Sub
Is this saying population increases by .0114 % a year? If so change 0.0114 to 1.0114 and you should be good I think.
Nope, still freezing.

 
Can you tell me why I am stuck in an infinite loop in these two programs?

Program 1- Trying to output the number of years required to double your principle with compound interest:

Code:
Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click        Dim amount, interest, years, newbalance, total As Double        amount = CDbl(txtAmount.Text)        interest = CDbl(txtInterest.Text)        Do While total <> (2 * amount)             newbalance = (amount * interest)            total = (newbalance * interest)            years += 1        Loop        txtYears.Text = CStr(years)     End Sub
Probably just want < instead of <>
 
Last edited by a moderator:
Program 2- Trying to output the year that the world population will reach 10 billion

Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click Dim pop As Double = 7000000000, newpop, numyears As Double Do While newpop < 10000000000 newpop = (pop * 0.0114) numyears = 2011 + 1 Loop txtOutput.Text = "World population will reach 10 Billion in " & numyears & "." End Sub
Is this saying population increases by .0114 % a year? If so change 0.0114 to 1.0114 and you should be good I think.
Nope, still freezing.
as mentioned above, the vale of pop is always 7000000000. 7000000000 x small number isn't greater then 10000000000 looping no matter how many times you loop

run the debug and step through

 
Program 2- Trying to output the year that the world population will reach 10 billion

Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click Dim pop As Double = 7000000000, newpop, numyears As Double Do While newpop < 10000000000 newpop = (pop * 0.0114) numyears = 2011 + 1 Loop txtOutput.Text = "World population will reach 10 Billion in " & numyears & "." End Sub
Is this saying population increases by .0114 % a year? If so change 0.0114 to 1.0114 and you should be good I think.
Nope, still freezing.
as mentioned above, the vale of pop is always 7000000000. 7000000000 x small number isn't greater then 10000000000 looping no matter how many times you loop

run the debug and step through
Yeah, I fixed that, but it's still getting stuck in a loop.

 
Program 2- Trying to output the year that the world population will reach 10 billion

Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click Dim pop As Double = 7000000000, newpop, numyears As Double Do While newpop < 10000000000 newpop = (pop * 0.0114) numyears = 2011 + 1 Loop txtOutput.Text = "World population will reach 10 Billion in " & numyears & "." End Sub
Is this saying population increases by .0114 % a year? If so change 0.0114 to 1.0114 and you should be good I think.
Nope, still freezing.
as mentioned above, the vale of pop is always 7000000000. 7000000000 x small number isn't greater then 10000000000 looping no matter how many times you looprun the debug and step through
Yeah, I fixed that, but it's still getting stuck in a loop.
how did you fix it?
 
Last edited by a moderator:
Program 2- Trying to output the year that the world population will reach 10 billion

Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click Dim pop As Double = 7000000000, newpop, numyears As Double Do While newpop < 10000000000 newpop = (pop * 0.0114) numyears = 2011 + 1 Loop txtOutput.Text = "World population will reach 10 Billion in " & numyears & "." End Sub
Is this saying population increases by .0114 % a year? If so change 0.0114 to 1.0114 and you should be good I think.
Nope, still freezing.
as mentioned above, the vale of pop is always 7000000000. 7000000000 x small number isn't greater then 10000000000 looping no matter how many times you looprun the debug and step through
Yeah, I fixed that, but it's still getting stuck in a loop.
how did you fix it?
Did what AAA said. Changed from .0114 to 1.0114

 
Change this line: total = (newbalance * interest)

To this: total = (total + newbalance)
Huh. It's not looping, but the result is wrong. Should be 24 years, but it outputted 67 years.
It's not compounding, how about something like this...

newbalance = amount

Do While total < (2 * amount)

newbalance = newbalance + (newbalance * interest)

total = (total + newbalance)

years += 1

Loop

 
Last edited by a moderator:
Change this line: total = (newbalance * interest)

To this: total = (total + newbalance)
Huh. It's not looping, but the result is wrong. Should be 24 years, but it outputted 67 years.
It's not compounding, how about something like this...

newbalance = amount

Do While total < (2 * amount)

newbalance = newbalance + (newbalance * interest)

total = (total + newbalance)

years += 1

Loop
That made the output 2 years.

 
Change this line: total = (newbalance * interest)

To this: total = (total + newbalance)
Huh. It's not looping, but the result is wrong. Should be 24 years, but it outputted 67 years.
It's not compounding, how about something like this...

newbalance = amount

Do While total < (2 * amount)

newbalance = newbalance + (newbalance * interest)

total = (total + newbalance)

years += 1

Loop
That made the output 2 years.
Try changing this: newbalance = newbalance + (newbalance * interest)

To this: newbalance = (newbalance * interest)

 
Change this line: total = (newbalance * interest)

To this: total = (total + newbalance)
Huh. It's not looping, but the result is wrong. Should be 24 years, but it outputted 67 years.
It's not compounding, how about something like this...

newbalance = amount

Do While total < (2 * amount)

newbalance = newbalance + (newbalance * interest)

total = (total + newbalance)

years += 1

Loop
That made the output 2 years.
Try changing this: newbalance = newbalance + (newbalance * interest)

To this: newbalance = (newbalance * interest)
I think that's where we started. Put me back in infinite loop.

 
Can you tell me why I am stuck in an infinite loop in these two programs?

Program 1- Trying to output the number of years required to double your principle with compound interest:

Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click Dim amount, interest, years, newbalance, total As Double amount = CDbl(txtAmount.Text) interest = CDbl(txtInterest.Text) Do While total <> (2 * amount) newbalance = (amount * interest) total = (newbalance * interest) years += 1 Loop txtYears.Text = CStr(years) End Sub
Somebody already pointed out that
Code:
Do While total <> (2 * amount)
needs to be < rather than <>. But, you get stuck in a loop because NewBalance never changes (because Amount and Interest are static), so Total never changes. You really should have it print out the key variables (NewBalance and Total) every time through the loop so you can see what is happening.

I'm too lazy to test it, but try:

Code:
        StartingAmount = amount        Do While total < (2 * StartingAmount )             amount= (amount * (1 + interest)            total = amount            years += 1        Loop
 
Last edited by a moderator:
Can you tell me why I am stuck in an infinite loop in these two programs?

Program 1- Trying to output the number of years required to double your principle with compound interest:

Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click Dim amount, interest, years, newbalance, total As Double amount = CDbl(txtAmount.Text) interest = CDbl(txtInterest.Text) Do While total <> (2 * amount) newbalance = (amount * interest) total = (newbalance * interest) years += 1 Loop txtYears.Text = CStr(years) End Sub
Somebody already pointed out that
Code:
Do While total <> (2 * amount)
needs to be < rather than <>.But, you get stuck in a loop because NewBalance never changes (because Amount and Interest are static), so Total never changes. You really should have it print out the key variables (NewBalance and Total) every time through the loop so you can see what is happening.

I'm too lazy to test it, but try:

StartingAmount = amount Do While total < (2 * StartingAmount ) amount= (amount * (1 + interest) total = amount years += 1 Loop
Doubling time = 23695 years.

 
Program 2- Trying to output the year that the world population will reach 10 billion

Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click Dim pop As Double = 7000000000, newpop, numyears As Double Do While newpop < 10000000000 newpop = (pop * 0.0114) numyears = 2011 + 1 Loop txtOutput.Text = "World population will reach 10 Billion in " & numyears & "." End Sub
newpop is always 7000000000 * .0114 since pop never changes.

newpop is unncessary, just use pop = pop * 1.0114

 
Last edited by a moderator:
Program 2- Trying to output the year that the world population will reach 10 billion

Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click Dim pop As Double = 7000000000, newpop, numyears As Double Do While newpop < 10000000000 newpop = (pop * 0.0114) numyears = 2011 + 1 Loop txtOutput.Text = "World population will reach 10 Billion in " & numyears & "." End Sub
newpop is always 7000000000 * .0114 since pop never changes.

newpop is unncessary, just use pop = pop * 1.0114
Hmm. It only added 1 year.

 
Program 2- Trying to output the year that the world population will reach 10 billion

Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click Dim pop As Double = 7000000000, newpop, numyears As Double Do While newpop < 10000000000 newpop = (pop * 0.0114) numyears = 2011 + 1 Loop txtOutput.Text = "World population will reach 10 Billion in " & numyears & "." End Sub
newpop is always 7000000000 * .0114 since pop never changes.

newpop is unncessary, just use pop = pop * 1.0114
Hmm. It only added 1 year.
numyears = numyears + 1

set numyears = 2011 before the do loop

 
Last edited by a moderator:
Program 2- Trying to output the year that the world population will reach 10 billion

Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click Dim pop As Double = 7000000000, newpop, numyears As Double Do While newpop < 10000000000 newpop = (pop * 0.0114) numyears = 2011 + 1 Loop txtOutput.Text = "World population will reach 10 Billion in " & numyears & "." End Sub
newpop is always 7000000000 * .0114 since pop never changes.

newpop is unncessary, just use pop = pop * 1.0114
Hmm. It only added 1 year.
numyears = numyears + 1

set numyears = 2011 before the do loop
Edit: Nevermind, that did it!! Thank you!

 
Last edited by a moderator:
Your interest formula was wrong; see this link And you don;t need the "newbalance" variable.

Do While total < (2 * amount)

years += 1
total = amount * Math.Pow(1 + interest, year)
Loop
 
Your interest formula was wrong; see this link And you don;t need the "newbalance" variable.

Do While total < (2 * amount)

years += 1
total = amount * Math.Pow(1 + interest, year)
Loop
That's giving me 25 years. Should be 24, but I'll take it.

Thanks for all your help on this.

 
Your interest formula was wrong; see this link And you don;t need the "newbalance" variable.

Do While total < (2 * amount)

years += 1
total = amount * Math.Pow(1 + interest, year)
Loop
That's giving me 25 years. Should be 24, but I'll take it.

Thanks for all your help on this.
make sure years = 0 before the loop starts, or move the year increment line below the calculation.

 
Your interest formula was wrong; see this link And you don;t need the "newbalance" variable.

Do While total < (2 * amount)

years += 1
total = amount * Math.Pow(1 + interest, year)
Loop
That's giving me 25 years. Should be 24, but I'll take it.

Thanks for all your help on this.
make sure years = 0 before the loop starts, or move the year increment line below the calculation.
I set years to -1 before the loop, and it still came to 25.

 

Users who are viewing this thread

Top