Home > front end >  How do I fix this for loop program?
How do I fix this for loop program?

Time:03-25

I'm stuck on this program where I have to create a for loop to predict the population after a given amount of days. Here it is:

Create an application that predicts the approximate size of a population of organisms. The application should use text boxes to allow the user to enter the starting number of organisms, the average daily population increase (as a percentage), and the number of days the organisms will be left to multiply. For example, assume the user enters the following values:

Starting number of organisms: 2
Average daily increase: 30%
Number of days to multiply: 10

It should output like this:

(Days) (Approximate Population)
1 ------- 2
2 ------- 2.6
3 ------- 3.38
4 ------- 4.394
5 ------- 5.7122

and so on.

But when I run my code this is all that appears:

(Days) (Approximate Population)
10 ------- 0

Here is the code:

public partial class Population : Form
{
    int count = 1;
    double organisms;
    double dailyIncrease;
    double days;
    double population;

    public Population()
    {
        InitializeComponent();
    }

    private void calculateButton_Click(object sender, EventArgs e)
    {
        try
        {
            organisms = double.Parse(organismsTextBox.Text);

            try
            {
                dailyIncrease = double.Parse(dailyIncreaseTextBox.Text);

                try
                {
                    days = double.Parse(daysTextBox.Text);

                    for (int count = 1; count <= days; count  )

                        population  = (organisms * dailyIncrease / 100);

                    populationListBox.Items.Add(days   "      "   population);

                }
                catch
                {
                    MessageBox.Show("Entires must be numeric.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    daysTextBox.Focus();
                    daysTextBox.SelectAll();
                }
            }
            catch
            {
                MessageBox.Show("Entries must be numeric.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dailyIncreaseTextBox.Focus();
                dailyIncreaseTextBox.SelectAll();
            }
        }
        catch
        {
            MessageBox.Show("Entries must be numeric", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            organismsTextBox.Focus();
            organismsTextBox.SelectAll();
        }

    }
}

I'm not sure what I'm doing wrong here?

CodePudding user response:

If your issue is the populationListBox only contains the last day item, try this:

for (int count = 1; count <= days; count  )
{                         
    population  = (population * dailyIncrease / 100);
    populationListBox.Items.Add(count   "      "   population);
}

CodePudding user response:

You can make it easier to read by using the xxx.TryParse() methods and checking for a failure instead of a success:

private void calculateButton_Click(object sender, EventArgs e)
{
    double organisms;
    double dailyIncrease;
    int days;

    if (!double.TryParse(organismsTextBox.Text, out organisms))
    {
        MessageBox.Show("Entries must be numeric", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        organismsTextBox.Focus();
        organismsTextBox.SelectAll();
        return;
    }

    if (!double.TryParse(dailyIncreaseTextBox.Text, out dailyIncrease))
    {
        MessageBox.Show("Entries must be numeric.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        dailyIncreaseTextBox.Focus();
        dailyIncreaseTextBox.SelectAll();
        return;
    }

    if (!int.TryParse(daysTextBox.Text, out days))
    {
        MessageBox.Show("Entires must be numeric.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        daysTextBox.Focus();
        daysTextBox.SelectAll();
        return;
    }

    populationListBox.Items.Clear();
    for (int count = 1; count <= days; count  )
    {
        populationListBox.Items.Add(count   "\t"   organisms);
        organisms  = (organisms * dailyIncrease / 100);        
    }
}

Example run:

enter image description here

CodePudding user response:

You shouldn't use exceptions to control the flow of your code. It makes it quite difficult to follow the logic of the code.

And since you are parsing doubles with exactly the same code you also have a lot of repetition that you can remove.

So, inside calculateButton_Click add this local method:

    bool TryParse(TextBox textBox, out double value)
    {
        if (double.TryParse(textBox.Text, out value))
        {
            return true;
        }
        MessageBox.Show("Entries must be numeric.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        textBox.Focus();
        textBox.SelectAll();
        value = default(double);
        return false;
    }

That handles the parsing for any TextBox you pass in.

Now the rest of the code for the calculateButton_Click is:

    if (TryParse(organismsTextBox, out double organisms))
    {
        if (TryParse(dailyIncreaseTextBox, out double dailyIncrease))
        {
            if (TryParse(daysTextBox, out double days))
            {
                double population = 0.0;
                for (int count = 1; count <= days; count  )
                {
                    population  = (organisms * dailyIncrease / 100.0);
                    populationListBox.Items.Add($"{days}      {population}");
                }
            }
        }
    }

This includes the code to add each day's population to the populationListBox.

  •  Tags:  
  • c#
  • Related