• HELP ASAP!! Infinite Loop? (VB.NET)

    Home » Forums » Developers, developers, developers » DevOps Lounge » HELP ASAP!! Infinite Loop? (VB.NET)

    Author
    Topic
    #412461

    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.

        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)
    
            '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
    Viewing 3 reply threads
    Author
    Replies
    • #902151

      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.

    • #902152

      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.

    • #902165

      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.

      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
      • #902179

        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.

        • #904536

          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

          • #908175

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

          • #908176

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

        • #904537

          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

      • #902180

        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.

    • #902166

      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.

      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
    Viewing 3 reply threads
    Reply To: HELP ASAP!! Infinite Loop? (VB.NET)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: