943,905 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 3093
  • C# RSS
Sep 10th, 2009
0

C# Test Score Data

Expand Post »
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:
C# Syntax (Toggle Plain Text)
  1. private void btadd_Click(object sender, EventArgs e)
  2. {
  3. try
  4. {
  5.  
  6. if (txtscore.Text == "")
  7. {
  8. MessageBox.Show("Score is a required field.", "Entry Error");
  9. }
  10. else
  11. {
  12. int score = Convert.ToInt32(txtscore.Text);
  13. if (score >= 0 && score <= 100)
  14. {
  15. int sumav = 0;
  16. foreach (int avgscore in scoreTotalArray)
  17. sumav += avgscore;
  18. int avg = sumav / scoreTotalArray.Length;
  19.  
  20. txtscoretotal.Text = score.ToString();
  21. //txtscorecount.Text = arraycount.ToString();
  22. txtaverage.Text = avg.ToString("d");
  23.  
  24. //add scores to array (and update index)
  25. scoreTotalArray[scoresIndex] = score;
  26. scoresIndex++;
  27.  
  28. //add scores to list
  29. scoreTotalList.Add(score);
  30. }
  31.  
  32. else
  33. {
  34. MessageBox.Show("Score must be greater than 0 and less than 100.", "Entry Error");
  35. }
  36. }
  37. }
  38.  
  39. catch (FormatException)
  40. {
  41. MessageBox.Show("Please enter a valid number for the Score.", "Entry Error");
  42. }
  43.  
  44. catch (IndexOutOfRangeException)
  45. {
  46. MessageBox.Show(
  47. "The index is out of range. Please exit the application",
  48. "Entry Error");
  49. }
  50. txtscore.Focus();
  51. }
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.
Last edited by EvilLinux; Sep 10th, 2009 at 3:13 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
EvilLinux is offline Offline
77 posts
since Sep 2009
Sep 10th, 2009
0

Re: C# Test Score Data

What version of visual studio and .NET framework are you using?
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 10th, 2009
0

Re: C# Test Score Data

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.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Sep 10th, 2009
0

Re: C# Test Score Data

Here is the way I would do it with the 3.5 framework and LINQ:
c# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace daniweb.scores
  11. {
  12. public partial class Form1 : Form
  13. {
  14. private List<int> scores;
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. scores = new List<int>();
  19. }
  20.  
  21. private void button1_Click(object sender, EventArgs e)
  22. {
  23. errorProvider1.SetError(textBox1, string.Empty);
  24. int score;
  25. if (string.IsNullOrEmpty(textBox1.Text))
  26. {
  27. errorProvider1.SetError(textBox1, "Please enter a score");
  28. return;
  29. }
  30. if (!int.TryParse(textBox1.Text, out score) || (score < 0) || (score > 100))
  31. {
  32. errorProvider1.SetError(textBox1, "You must enter a value between 0 and 100");
  33. return;
  34. }
  35.  
  36.  
  37. AddScore(score);
  38.  
  39. }
  40.  
  41. private void AddScore(int score)
  42. {
  43. scores.Add(score);
  44. listBox1.Items.Add(score);
  45. }
  46.  
  47. private void DisplayScores()
  48. {
  49. scores.Sort();
  50. listBox1.BeginUpdate();
  51. listBox1.Items.Clear();
  52. foreach (int i in scores)
  53. listBox1.Items.Add(i);
  54. listBox1.EndUpdate();
  55. }
  56.  
  57. private void ClearScores()
  58. {
  59. listBox1.Items.Clear();
  60. }
  61.  
  62. private void Form1_Load(object sender, EventArgs e)
  63. {
  64. ClearScores();
  65. }
  66.  
  67. private void button2_Click(object sender, EventArgs e)
  68. {
  69. int maxScore = scores.Max();
  70. int minScore = scores.Min();
  71. double avgScore = scores.Average();
  72. int cnt = scores.Count;
  73.  
  74. StringBuilder sb = new StringBuilder();
  75. sb.AppendLine(string.Format("Max Scores : {0:F0}", maxScore));
  76. sb.AppendLine(string.Format("Avg Scores : {0:F2}", avgScore));
  77. sb.AppendLine(string.Format("Min Scores : {0:F0}", minScore));
  78. sb.AppendLine(string.Format("Total Scores: {0:F0}", cnt));
  79. MessageBox.Show(sb.ToString());
  80. }
  81. }
  82. }
Attached Files
File Type: zip daniweb.scores.zip (13.3 KB, 66 views)
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 10th, 2009
0

Re: C# Test Score Data

Click to Expand / Collapse  Quote originally posted by ddanbe ...
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?

Click to Expand / Collapse  Quote originally posted by sknake ...
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 .

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.
Attached Thumbnails
Click image for larger version

Name:	form.png
Views:	326
Size:	75.5 KB
ID:	11559  
Last edited by EvilLinux; Sep 10th, 2009 at 3:55 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
EvilLinux is offline Offline
77 posts
since Sep 2009
Sep 10th, 2009
1

Re: C# Test Score Data

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...
Attached Files
File Type: zip Scorestest.zip (46.2 KB, 133 views)
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Sep 10th, 2009
0

Re: C# Test Score Data

Click to Expand / Collapse  Quote originally posted by ddanbe ...
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!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
EvilLinux is offline Offline
77 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: C# flash manipulation
Next Thread in C# Forum Timeline: complications with XML serialization





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC