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);
}
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.