I have a calculator that is supposed to take in scores, compute score total,score count,and score average. I am also to create a list that will store all the scores that have been entered and display them in msgbox which I have already done and have working. My problem is that I cant figure out how to use the ".count" property of a list. I attempted it and when I add in a score it starts at zero instead of one. Also if anyone would be able to help me on how I might get the score total and average to work as well. Thanks

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

namespace ScoreCalculator
{
    public partial class frmScoreCalculator : Form
    {

        public frmScoreCalculator()
        {
            InitializeComponent();
        }

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


        private void btnAdd_Click(object sender, EventArgs e)
        {

            int intScore = 0;
            Int32.TryParse(txtScore.Text,out intScore);

            //int intScoreCount = 0;
            txtScoreCount.Text = intScoresList.Count.ToString();    

            intScoresList.Add(intScore);
            txtScore.Focus();
            txtScore.SelectAll();
        }


        private void btnDisplayScores_Click(object sender, EventArgs e)
        {
            intScoresList.Sort();

            string DisplayScore = "";

            foreach (int score in intScoresList)
            {
                DisplayScore += score.ToString() + "\n";
            }

            MessageBox.Show(DisplayScore, "Sorted Scores");

            txtScore.Focus();
        }


        private void btnClearScores_Click(object sender, EventArgs e)
        {
            intScoresList.Clear(); 
            this.txtScore.Clear();
            this.txtScoreCount.Clear();
            this.txtAverage.Clear();
            this.txtScoreTotal.Clear();
            //this.intScoreCount = 0;
            //this.intScoreTotal = 0;
            txtScore.Focus();
        }


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


    }
}

Recommended Answers

All 19 Replies

Hello jd3189
Yep. You'll find that most collections are zero-based, in programming. :) But not to worry, sometimes all you need to do is add 1 to adjust the indeces (or in this case the count) of the "list", to make it more "human readable".

In your case however the Count property should return the actual count of elements in your list (i.e. - if there are 5 elements in the list the Count property will return 5).

I think the issue with your code is in where you call

intScoresList.Count.ToString(); 

Take a look at your code again and think about the order in which events are happening.

Now, as far as help with getting the total and the average, show us what you have so far and we'll guide you. I will give you this hint: calculate the total first. That will help in calculating the average.

Tekkno

I just cant seem to figure it out. Here is what I have so far and it still wont work.

 private void btnAdd_Click(object sender, EventArgs e)
        {

            int intScore = 0;
            Int32.TryParse(txtScore.Text,out intScore);

            //int intScoreCount = 0;
            txtScoreCount.Text = intScoresList.Count.ToString();


            //int sum = 0;
            //for (int i = 0; i < intScoresList.Length; i++)
                //sum += intScoresList[i];
           // int Average = sum / intScoreCount;

            int sum = 0;
            foreach (int total in intScoresList)
                sum += total;
            int average = sum / intScoresList.Count;

            txtAverage.Text = average.ToString();
            txtScoreTotal.Text = sum.ToString();


            intScoresList.Add(intScore);
            txtScore.Focus();
            txtScore.SelectAll();
        }

Few remarks:
1) Why does a btnAdd_Click seems to be calculating an average?
An Add button must only do one thing : ADD, point.
2) You may prefer it but you one time or another will get into trouble with it.

//this is allowed, but avoid it
 foreach (int total in intScoresList)
    sum += total;
//instead always use
 foreach (int total in intScoresList)
 {
     sum += total;
 }

 //perhaps you think it is silly, don't

3)Instead of saying it does not work, I and others still like to know what is not working.

The add button does all the computations simultaneously. So when I add a score into it it will go ahead and calculate everything. That is not the issue. The first problem is the count property which I am not sure If I am using correctly or not. Instead of starting at 1 it starts at zero when I add a number in and I dont know how to fix that. Secondly, int average = sum / intScoresList.Count; is giving me a divide by zero exception.

If intScoresList.Count is zero, you added nothing to your list of scores.
Find out why. Or show the code where it should get done.

Fixed! Now everything is working. Thanks!

That is not the issue.
It is!
On line 16 of your last code you set sum=0 every time you push Add!
Your Add button must just add or perhaps update the Count text field as I previously said.
I should create a new button "Calculate" and calculate the average there.

I see what your saying and I would agree however I cant do that, the assignment doesn't say to put a calculate button on it. Only add,clear,display score, and exit.

Calculate = display score I guess.

Hello again jd3189.
Just to confirm, did you get everything working (totals and averages)? Mind if I take a look at your working code?

Tekkno

Tekkno here is my working code. I am also needing to do data validation and exception handling. I attempted the exception handling but it did not work. Would you be able to assist me with that?

 public partial class frmScoreCalculator : Form
    {

        public frmScoreCalculator()
        {
            InitializeComponent();
        }

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



        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
            int intScore = 0;
            Int32.TryParse(txtScore.Text,out intScore);
            intScoresList.Add(intScore);

            txtScoreCount.Text = intScoresList.Count.ToString();


            int sum = 0;
            foreach (int total in intScoresList)
                sum += total;
            int average = sum / intScoresList.Count;

            txtAverage.Text = average.ToString();
            txtScoreTotal.Text = sum.ToString();


            txtScore.Focus();
            txtScore.SelectAll();


            }
            catch (DivideByZeroException)  
            {
                MessageBox.Show(
                    "Divide-by-zero error has occurred. Please enter a non-zero value for operand 2.",
                    "Entry Error");
            }
            catch (OverflowException) 
            {
                MessageBox.Show(
                    "An overflow exception has occurred. Please enter smaller values.",
                    "Entry Error");
            }
            catch (FormatException)   
            {
                MessageBox.Show(
                    "A format exception has occurred. Please check all entries.",
                    "Entry Error");
            }              
            catch (Exception ex)     
            {
                MessageBox.Show(ex.Message + "\n\n" +
                           ex.GetType().ToString() + "\n" +
                           ex.StackTrace, "Exception");
            }


        }




        private void btnDisplayScores_Click(object sender, EventArgs e)
        {
            intScoresList.Sort();

            string DisplayScore = "";

            foreach (int score in intScoresList)
            {
                DisplayScore += score.ToString() + "\n";
            }

            MessageBox.Show(DisplayScore, "Sorted Scores");

            txtScore.Focus();
        }


        private void btnClearScores_Click(object sender, EventArgs e)
        {
            intScoresList.Clear(); 
            this.txtScore.Clear();
            this.txtScoreCount.Clear();
            this.txtAverage.Clear();
            this.txtScoreTotal.Clear();
            //this.intScoreCount = 0;
            //this.intScoreTotal = 0;
            txtScore.Focus();
        }


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


    }

I'll try it out on my home computer when i get off shift. In the meantime I'll try to trace through the code when I have a free moment. :)

Tekkno

My apologies. I wasn't able to take a look at this after work yesterday as I planned. Do you still need help?

Tekkno

No problem. I could still use some help on this would you recommend starting a new thread or would you be able to assit me?

I'll be able to assist you when I get home after my shift. I'll take a look at the code and see where I can help :)

Tekkno

Hello jd3189!
Can you tell me which data you're trying to validate? I see your exception handling code, and one thing that stands out, to me, is that you don't include a variable with your exception. For example:

catch (DivideByZeroException)
    {
    ...
    }

The way I'd learned to code this was:

 catch (DivideByZeroException divByZ)
    {
        ...
    }

However, I'm still using VS 2010 and it may be that (if you're using VS 2013) it's permissible in VS 2013.

Would it be possible for you to zip (compress) your enitre project and e-mail it to me, so that I could try to run it on my system. This way I cand better assist.

Tekkno

For some reason my email wont let me send a zipped file I'm not sure what the deal is. Let's just say the only textbox that lets you input a number is txtScore. So this is the only one I need to check and see if there are exceptions. The main things I will need to check for is that the scores entered are between 0-100 and that there isent any funky data being inputed like a letter. I did not mean to add the divide by zero one because I realized there is no division that the user would have to do.

I just figured out what I was doing wrong so I got it working now

AWESOME!
Glad you were able to figure it out. :)

Tekkno

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.