1

I'm trying to display 5 scores in an array, but unfortunately all I get in the message box for the results is 0.

Any help would be appreciated.

public partial class Form1 : Form
{
    private int[] scoresArray = new int[5];
    private int scoreTotal = 0;
    private int scoreCount = 0;

    public Form1()
    {
        InitializeComponent();
    }

when the add button is clicked the scores are stored in the array up to 5 times.

    private void btnAdd_Click(object sender, EventArgs e)
    {

        try
        {
            if (txtScore.Text == "")
            {
                MessageBox.Show("Score is required", "Entry Error");
            }

            else
            {
                int score = Convert.ToInt32(txtScore.Text);
                decimal average = 0;

                if (score >= 0 && score <= 100)
                {
                    if (scoreCount != 4)
                    {
                        scoreTotal += score;
                        scoresArray[scoreCount] = score;
                        scoreCount++;
                        average = scoreTotal / scoreCount;
                    }
                    else
                    {
                        MessageBox.Show("Array is full");
                    }

                    txtScoreTotal.Text = scoreTotal.ToString();
                    txtScoreCount.Text = (scoreCount + 1).ToString();
                    txtAverage.Text = average.ToString();
                }

                else
                {
                    MessageBox.Show("Score must be greater than 0 and less than or equal to 100.", "Entry Error");
                }
            }
        }

        catch (FormatException)
        {
            MessageBox.Show("Please enter a valid number for the Score field.", "Entry Error");
        }

        txtScore.Focus();
    }

    private void btnDisplayScores_Click(object sender, EventArgs e)
    {
        string message = "";

        for (int i = 0; i < scoresArray.Length; i++)
        {
            message = scoresArray[i].ToString() + "\n";
        }

        MessageBox.Show(message, "Scores");
    }

1 Answer 1

4

You keep overwriting message in this loop:

for (int i = 0; i < scoresArray.Length; i++)
{
    message = scoresArray[i].ToString() + "\n";
}

So it's only ever going to show the last value. You probably want to append to it instead:

for (int i = 0; i < scoresArray.Length; i++)
{
    message += scoresArray[i].ToString() + "\n";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks it worked, but now it shows extra zero's for when you don't enter scores.
@Benyamin: Well, you have an array of length 5. It's always going to be of length 5, whether you update all 5 values or not. And 0 is the default for an integer. If the length of the collection needs to be dynamic, you might try something like a List<int> instead of an array. That way if, for example, only 3 scores are entered then you can dynamically add only 3 elements to the collection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.