I have the same assignment someone else ask help with. The assignment calls for a test score calculator that 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. There is also a Clear button to have all entries removed.

My program is a complete mess, as it will not total the scores as it should, each iteration is counted as 2, and it will not calculate the average either. The code I have so far is below:

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 TestScoresCalc
{
    public partial class frmScoreCalculator : Form
    {
        public frmScoreCalculator()
        {
            InitializeComponent();
        }

        List<int> scores = new List<int>();

        private void btnAdd_Click(object sender, EventArgs e)
        {

        //List<int> scores = new List<int>();
        scores.Add(20);
            int sum = 0;
            for (int i = 0; i < scores.Count; i++)
            {
                int score = scores[i];
                sum += score;
            }

            if (txtScore.Text == "")
            {
                txtScore.Focus();
                return;
            }

            else
            {

                int score = int.Parse(txtScore.Text);
                int Total = 0;
                int Average = 0;

                Total = score;
                Average = Total / scores.Count;
                txtScoreTotal.Text = Total.ToString();
                txtAverage.Text = Average.ToString("");
                txtScoreCount.Text = scores.Count.ToString("");


                txtScore.Text = "";
                txtScore.Focus();
            }
        }


        private void btnDisplayScores_Click(object sender, EventArgs e)
        {
            scores.Sort();                 
            string scoresString = "List of all scores entered :\n\n";
            foreach (int i in scores)
                scoresString += i + "\n";
            MessageBox.Show(scoresString, "Sorted Scores");
        }

        private void btnClearScores_Click(object sender, EventArgs e)
        {
            scores.Clear();
            txtScoreTotal.Text = "0";
            txtScoreCount.Text = "0";
            txtAverage.Text = "0";
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }      






        }
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

Hi WhiteTigerclaws, welcome to DANIWEB.
I you post code please do it between code tags.
Have not much time now but :

private void btnDisplayScores_Click(object sender, EventArgs e)
{
  scores.Sort(); 
  string scoresString = "List of all scores entered :\n\n";
  foreach (int i in scores)
    scoresString += i + "\n";
  MessageBox.Show(scoresString, "Sorted Scores");
}

Here line 6 should read : scoresString += i.ToString() + "\n";

Hi WhiteTigerclaws, welcome to DANIWEB.
I you post code please do it between code tags.
Have not much time now but :

private void btnDisplayScores_Click(object sender, EventArgs e)
{
  scores.Sort(); 
  string scoresString = "List of all scores entered :\n\n";
  foreach (int i in scores)
    scoresString += i + "\n";
  MessageBox.Show(scoresString, "Sorted Scores");
}

Here line 6 should read : scoresString += i.ToString() + "\n";

Thanks for the tip. Here is a corrected code which works, but I need to validate the input to be between 0 to 100 I'm trying and if

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 TestScoresCalc
{
    public partial class frmScoreCalculator : Form

    {
        List<int> scores = new List<int>();
        
        private int Total = 0;
        private int Average = 0;
        public frmScoreCalculator()
        


        {
            InitializeComponent();           

        }
           
        private void btnAdd_Click(object sender, EventArgs e)
        {
            
            
            try
            {
                int score = Convert.ToInt32(txtScore.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("Please enter a valid score", "Input Error");
            }


        
        scores.Add(20);
            int sum = 0;
            for (int i = 0; i < scores.Count; i++)
            {
                int score = scores[i];
                sum += score;
            }

            if (txtScore.Text == "")
            {
                txtScore.Focus();
                return;
            }

            else
            {

                int score = int.Parse(txtScore.Text);
                //int Total = 0;
                //int Average = 0;
                
                Total = score + Total;
                Average = Total / scores.Count;
                txtScoreTotal.Text = Total.ToString();
                txtAverage.Text = Average.ToString("");
                txtScoreCount.Text = scores.Count.ToString("");
                
                
                txtScore.Text = "";
                txtScore.Focus();
            }
        }
                   
                      
      private void btnDisplayScores_Click(object sender, EventArgs e)
   
      {
   
      scores.Sort();   
      string scoresString = "";
      foreach (int i in scores)
        scoresString += i + "\n";
      MessageBox.Show(scoresString, "Sorted Scores");
   
      }

        

        private void btnClearScores_Click(object sender, EventArgs e)
        {
            scores.Clear();
            txtScoreTotal.Text = "0";
            txtScoreCount.Text = "0";
            txtAverage.Text = "0";
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }      
        
              

     


        }
}

Try this:

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;
using System.Text;

namespace daniweb
{
  public partial class frmScoreCalculator : Form
  {
    List<int> scores = new List<int>();
    private int Total = 0;
    private int Average = 0;

    public frmScoreCalculator()
    {
      InitializeComponent();
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
      int scoreInput;
      if (string.IsNullOrEmpty(txtScore.Text) || !int.TryParse(txtScore.Text, out scoreInput))
      {
        txtScore.Focus();
        MessageBox.Show("Please enter a valid score", "Input Error");
        txtScore.Focus(); //in case they double click the message and select another ctrl
        return;
      }
      else if ((scoreInput < 0) || (scoreInput > 100))
      {
        txtScore.Focus();
        MessageBox.Show("The score must be between 0 and 100", "Input Error");
        txtScore.Focus(); //in case they double click the message and select another ctrl
        return;
      }

      scores.Add(20); //Is this right, shouldnt it be the input??
      int sum = 0;
      for (int i = 0; i < scores.Count; i++)
      {
        int score = scores[i];
        sum += score;
      }

      Total = scoreInput + Total;
      Average = Total / scores.Count;
      txtScoreTotal.Text = Total.ToString();
      txtAverage.Text = Average.ToString(string.Empty);
      txtScoreCount.Text = scores.Count.ToString(string.Empty);
      txtScore.Text = string.Empty;
      txtScore.Focus();
    }

    private void btnDisplayScores_Click(object sender, EventArgs e)
    {
      scores.Sort();
      StringBuilder sb = new StringBuilder();
      for (int i1 = 0; i1 < scores.Count; i1++)
        sb.AppendLine(i1.ToString());
      MessageBox.Show(sb.ToString().Trim(), "Sorted Scores");
    }

    private void btnClearScores_Click(object sender, EventArgs e)
    {
      scores.Clear();
      txtScoreTotal.Text = "0";
      txtScoreCount.Text = "0";
      txtAverage.Text = "0";
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
      this.Close();
    }
  }
}

whitetigerclaws,

Thank you for the rep :) Did you get everything working? If the code provided by danny and myself helped solve your issue please mark this thread as solved, if not let us know what you need help with. Good luck!

Seems I learned something new here!
scoresString += i.ToString() +"\n";is the same as
scoresString += i +"\n";:-/
The cast apparantly happens automatic...
The way Scott did it is in fact to be preferred.

whitetigerclaws,

Thank you for the rep :) Did you get everything working? If the code provided by danny and myself helped solve your issue please mark this thread as solved, if not let us know what you need help with. Good luck!

All calculations are now working except the Display Scores button when press the message box appears but rather than numbers like, 98, 89, 75 and 99 which I input, it list 0, 1, 2, 3, and 4. I have not been able to solve this one yet, and unfortunately I have been awake for 22.5 hrs. so I need to take a break and get back to it later. If you can help me solve it will be most appreciated. :zzz:

You guys are all great. I hope one day I can get half as good as you! :-/

Scott left in a little puzzle to solve for you (Yeah the guy is like that:D) Look carefully at line 64 and figure out what is happening.
If you can't find the answer we will be most kind to provide it to you, but it's more fun to find out yourself!

I'm working on a similar project. When I tried working with Scott's code, it executed, however, the buttons werent functioning. Nothing was responsive. What am I doing wrong?

Try this:

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;
using System.Text;

namespace daniweb
{
  public partial class frmScoreCalculator : Form
  {
    List<int> scores = new List<int>();
    private int Total = 0;
    private int Average = 0;

    public frmScoreCalculator()
    {
      InitializeComponent();
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
      int scoreInput;
      if (string.IsNullOrEmpty(txtScore.Text) || !int.TryParse(txtScore.Text, out scoreInput))
      {
        txtScore.Focus();
        MessageBox.Show("Please enter a valid score", "Input Error");
        txtScore.Focus(); //in case they double click the message and select another ctrl
        return;
      }
      else if ((scoreInput < 0) || (scoreInput > 100))
      {
        txtScore.Focus();
        MessageBox.Show("The score must be between 0 and 100", "Input Error");
        txtScore.Focus(); //in case they double click the message and select another ctrl
        return;
      }

      scores.Add(20); //Is this right, shouldnt it be the input??
      int sum = 0;
      for (int i = 0; i < scores.Count; i++)
      {
        int score = scores[i];
        sum += score;
      }

      Total = scoreInput + Total;
      Average = Total / scores.Count;
      txtScoreTotal.Text = Total.ToString();
      txtAverage.Text = Average.ToString(string.Empty);
      txtScoreCount.Text = scores.Count.ToString(string.Empty);
      txtScore.Text = string.Empty;
      txtScore.Focus();
    }

    private void btnDisplayScores_Click(object sender, EventArgs e)
    {
      scores.Sort();
      StringBuilder sb = new StringBuilder();
      for (int i1 = 0; i1 < scores.Count; i1++)
        sb.AppendLine(i1.ToString());
      MessageBox.Show(sb.ToString().Trim(), "Sorted Scores");
    }

    private void btnClearScores_Click(object sender, EventArgs e)
    {
      scores.Clear();
      txtScoreTotal.Text = "0";
      txtScoreCount.Text = "0";
      txtAverage.Text = "0";
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
      this.Close();
    }
  }
}
This question has already been answered. Start a new discussion instead.