Hi,

I've been working on this project I'm doing and its killing me because I can't figure why my array\formula's don't want to get along.

Here is the general overview of what the project requires:
Here the project:
Operation
• The user enters a score and clicks the Add button or presses the Enter key.
• The application calculates and displays the score total, score count, and average score.
• If the user clicks the Display scores button, the application sorts the scores and displays them in a message box.
• If the user clicks the Clear scores button, the application removes all the scores and clears the score, score total, score count, and average controls.
Specifications
• This application should be able to handle at least 20 scores.
• This application should check the number entered by the user to make sure it is a valid integer from 0 to 100.

So far the code I have is below:

private void btadd_Click(object sender, EventArgs e)
        {
            try
            {

                if (txtscore.Text == "")
                {
                    MessageBox.Show("Score is a required field.", "Entry Error");
                }
                else
                {
                    int score = Convert.ToInt32(txtscore.Text);
                    if (score >= 0 && score <= 100)
                    {
                        int sumav = 0;
                        foreach (int avgscore in scoreTotalArray)
                            sumav += avgscore;
                        int avg = sumav / scoreTotalArray.Length;

                        txtscoretotal.Text = score.ToString();
                        //txtscorecount.Text = arraycount.ToString();
                        txtaverage.Text = avg.ToString("d");
                        
                        //add scores to array (and update index)
                        scoreTotalArray[scoresIndex] = score;
                        scoresIndex++;

                        //add scores to list
                        scoreTotalList.Add(score);
                    }

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

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

            catch (IndexOutOfRangeException)
            {
                MessageBox.Show(
                    "The index is out of range. Please exit the application",
                    "Entry Error");
            }
            txtscore.Focus();
        }

If anyone can help figure what I'm doing wrong or maybe point me correct direction, my book hasn't help worth crap other than giving me a vague basic idea of what I need.

Thanks in advance.

What version of visual studio and .NET framework are you using?

Your btadd_Click method should just do what it says: add a score to a list. Display can first perform the calculations and display the results.
Clear button should only clear the list.

Here is the way I would do it with the 3.5 framework and LINQ:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb.scores
{
  public partial class Form1 : Form
  {
    private List<int> scores;
    public Form1()
    {
      InitializeComponent();
      scores = new List<int>();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      errorProvider1.SetError(textBox1, string.Empty);
      int score;
      if (string.IsNullOrEmpty(textBox1.Text))
      {
        errorProvider1.SetError(textBox1, "Please enter a score");
        return;
      }
      if (!int.TryParse(textBox1.Text, out score) || (score < 0) || (score > 100))
      {
        errorProvider1.SetError(textBox1, "You must enter a value between 0 and 100");
        return;
      }


      AddScore(score);
      
    }

    private void AddScore(int score)
    {
      scores.Add(score);
      listBox1.Items.Add(score);
    }

    private void DisplayScores()
    {
      scores.Sort();
      listBox1.BeginUpdate();
      listBox1.Items.Clear();
      foreach (int i in scores)
        listBox1.Items.Add(i);
      listBox1.EndUpdate();
    }

    private void ClearScores()
    {
      listBox1.Items.Clear();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      ClearScores(); 
    }

    private void button2_Click(object sender, EventArgs e)
    {
      int maxScore = scores.Max();
      int minScore = scores.Min();
      double avgScore = scores.Average();
      int cnt = scores.Count;

      StringBuilder sb = new StringBuilder();
      sb.AppendLine(string.Format("Max Scores  : {0:F0}", maxScore));
      sb.AppendLine(string.Format("Avg Scores  : {0:F2}", avgScore));
      sb.AppendLine(string.Format("Min Scores  : {0:F0}", minScore));
      sb.AppendLine(string.Format("Total Scores: {0:F0}", cnt));
      MessageBox.Show(sb.ToString());
    }
  }
}

Your btadd_Click method should just do what it says: add a score to a list. Display can first perform the calculations and display the results.
Clear button should only clear the list.

I think I'm a bit confused on what you mean?
When the btadd_Click method is run it add the enter score to the array which would be the list correct? What do you mean by the Display can first perform the calculations?

What version of visual studio and .NET framework are you using?

VS 2008 Pro .NET 3.5 - Only reason I'm using this is because my work has copies of it and I've done a bit of programming in the past so trying to relearn it all and keep up to date if you were curious.

Maybe I'm confusing myself more.
My form has 4 text boxes:
Scores - [user enter value]
Score Total - [Read only]
Score Count [Read only] [How many times a value has been entered]
Score Average - [Read Only]

Then I have 4 buttons:
Add [add's the user's input]
Display Score [Sorts them from lower value to highest, I got the code for that and it works]
Clear Score [Clears everything on the form including the array]
Exit [exit the form]

The image below is the form I have setup. I'm also using array's to hold the data, I guess I'm confused on that a bit also :S.

On a random side note question: Did you guys learn C# from taking a class or just own your on time and reading other people's stuff. Because I'm taking a class, and honestly am not learning a whole lot trying to read a book 100+ times a week.

I mean the Display button click handler could perform the calculations.
But I think I have seen you or one of your friends before...
I had the following ready to post...

commented: solved! +13

I mean the Display button click handler could perform the calculations.
But I think I have seen you or one of your friends before...
I had the following ready to post...

You are like my hero. That is totally what I need to do. And the fact now looking at it, and look at what I had I see what I'm doing wrong and how I can fix mine.

Thank you everyone for the quick support and help you guys are life savers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.