do-while loop freezing program

126 views Asked by At

I try using the do-while loop but it doesn't do anything after I enter the input and initiate the button click event. It is supposed to calculate out the amount and list for all the years following until it is <=40,000. I can get the program to run without the loop but not with it.

    private double InterestEarned(double AMT, double AIR = 0.07)
    {                        
        return AMT * AIR;
    }

    private double InheritanceAmount(double BAL, double IR, double AIR = 0.07)
    {            
        return (BAL * IR * AIR) - 40000;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            double AMT;
            AMT = (double.Parse(textBox1.Text));

            if (radioButton1.Checked==true)
            {
                do
                {
                const double IR3 = 0.03;
                double BAL, IR, earn;
                int year = 2014;

                AMT = (double.Parse(textBox1.Text));
                IR = IR3;
                year++;
                BAL = InheritanceAmount(AMT, IR);
                earn = InterestEarned(AMT);

                listBox1.Items.Add("You have chosen a 3% inflation rate. Your investment starts at" + AMT.ToString("C") + " and earn 7% a year. You withdraw  $40,000 a year.");
                listBox1.Items.Add("Year" + "\t" + "Interest Earned" + "\t" + "Balance");
                listBox1.Items.Add(year++ + "\t" + earn.ToString("C") + "\t" + BAL.ToString("C"));
                } while (AMT > 40000);
            }
                else if (radioButton2.Checked==true)
                {
                    do
                    {
                    const double IR4 = 0.04;
                    double BAL, IR, earn;
                    int year = 2014;

                    AMT = (double.Parse(textBox1.Text));
                    IR = IR4;
                    year++;
                    BAL = InheritanceAmount(AMT, IR);
                    earn = InterestEarned(AMT);

                    listBox1.Items.Add("You have chosen a 4% inflation rate. Your investment starts at" + AMT.ToString("C") + " and earn 7% a year. You withdraw  $40,000 a year.");
                    listBox1.Items.Add("Year" + "\t" + "Interest Earned" + "\t" + "Balance");
                    listBox1.Items.Add(year++ + "\t" + earn.ToString("C") + "\t" + BAL.ToString("C"));
                    } while (AMT > 40000);
                }
            else
            {                   
                MessageBox.Show("Please select an inflation rate.");                    
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
2

There are 2 answers

1
Tim On

The do...while loop is conditional on the value of AMT being greater than 40000. But the value of AMT only comes from the user (via the textbox) and is never changed again. So the loop just happens forever (which, since it is being run on your UI thread, will lock the UI). Either your condition is wrong or you need to be changing the value of AMT within the loop.

5
The_Black_Smurf On

Your while condition is based on the AMT variable (> 40000). You initialize that variable correctly before the do while. However, you also reinitialize that variable inside the do while loop and this is why the variable never reach the condition that would make it exit the your do while loop.

The first thing to do here is to comment the line where the AMT variable is set back to it's original value:

do
{
  const double IR3 = 0.03;
  double BAL, IR, earn;
  int year = 2014;

  //Comment the line below
  //AMT = (double.Parse(textBox1.Text));
  IR = IR3;
  year++;
  BAL = InheritanceAmount(AMT, IR);
  earn = InterestEarned(AMT);

  //...

} while (AMT > 40000);

The next thing you should do is to increase the AMT based on the earn value:

AMT += earn;

The last thing you should consider is way to avoid infinite loop when the AMT original value is <= 0 or when the interest are set to 0.