0

If their is anyone who can help me I'm having a problem in my score calculator in which it's suppose to calculate the user input of as many numbers they wish giving the score total, score count, and score average but when the user enters the numbers they wish to add it also adds the score total how can I stop it from adding the score total? PLEASE HELP!

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 ScoreCalculatorProj
{
    public partial class frmScoreCalculator : Form
    {
        // Creates the list where user can enter the score as many times as they please
        List<decimal> scores = new List<decimal>();
        decimal scoreaverage = 0;
        decimal scoretotal = 0;
        decimal scorestandarddev = 0;
        decimal score;
        public frmScoreCalculator()
        {
            InitializeComponent();
        }

        // Pulls out the about form when user clicks the about button
        private void btnAbout_Click(object sender, EventArgs e)
        {
            frmAbout form = new frmAbout();
            form.Show();
        }

        // Button so user input could be accepted for addition process
        // Calculates the addition of the scores entered by the user
        private void btnAdd_Click(object sender, EventArgs e)
        {
            score = decimal.Parse(txtScore.Text);
            scores.Add(score);

            for (int i = 0; i < scores.Count; i++)
            {
        // Calculates the score user input to receive  score total
                scoretotal += scores[i];

            }
        // Calculates the score average of the users score input
            scoreaverage = scoretotal / scores.Count;

        // Displays the scoreaverage, scorecount, score total, as well as recieves users score input
            txtScoreaverage.Text = scoreaverage.ToString();
            txtScorecount.Text = scores.Count.ToString();
            txtScoretotal.Text = scoretotal.ToString();
            txtScore.Text = "";
            txtScore.Focus();
        }

        // Button to display the user score input in a sorted order
        private void btnDisplayScores_Click(object sender, EventArgs e)
        {
            string numbersString = "";
            foreach (decimal score in scores)
                {
                numbersString += score + "\n";
            }
            MessageBox.Show(numbersString, "Sorted Scores");
        }

        // Button to clear the textbox, output labels, items from the list box, and elements of the list
        private void btnClearScores_Click(object sender, EventArgs e)
        {

            txtScore.Text = "";
            txtScore.Focus();
            scores.Clear();
            scoreaverage = 0;
            scoretotal = 0;
            scorestandarddev = 0;
            score = 0;
            txtScoreaverage.Text = "";
            txtScorecount.Text = "";
            txtScoretotal.Text = "";
            txtScorestandarddev.Text = "";
        }

        // Button to exit program
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

    }
}
2
Contributors
3
Replies
23
Views
2 Months
Discussion Span
Last Post by tinstaafl
0

Your problem appears to be that your are using scoretotal to accumulate the total, but you are re-adding each previous score everytime you add a score. The simplest fix would be to eliminate the loop and just add score to scoretotal each time you add it to the list:

    private void btnAdd_Click(object sender, EventArgs e)
    {
        score = decimal.Parse(txtScore.Text);
        scores.Add(score);
        scoretotal += score;
        // Calculates the score average of the users score input
        scoreaverage = scoretotal / scores.Count;
        // Displays the scoreaverage, scorecount, score total, as well as recieves users score input
        txtScoreaverage.Text = scoreaverage.ToString();
        txtScorecount.Text = scores.Count.ToString();
        txtScoretotal.Text = scoretotal.ToString();
        txtScore.Text = "";
        txtScore.Focus();
    }

Some things I noticed:

When you are accepting input from a user, always allow for the user to make a mistake in the input. The way your code is right now, any mistake, that isn't a valid decimal number, will cause it to crash. You have several options, that I see , for controlling numeric input:

  • Use the decimal.TryParse method instead of decimal.Parse.
  • Use a NumericUpdown control instead of a TextBox
  • Use a Combobox filled with acceptable values(this one may not be suitable for this particular application)

When you are naming your controls, it has been found that including the type of control in the name can create major complications in a large project, if you decide to change the type of control. You will be better off just keeping your names descriptive of their use.

Since the default Form class includes the ControlBox, to allow the user to exit from the program, it is superfluous to add a button for this. I would suggest only using it on a form that doesn't have the ControlBox enabled.

Edited by tinstaafl

0

Then how would I add score to scoretotal after eliminating the loop? Can you please show me how to do it.
Rechecked sorry I did it now thank you for the info.

Edited by Mario_6

1

You are very welcome. If this has solved your problem please remember to mark this solved.

Edited by tinstaafl

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.