C# Test Score Data

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2009
Posts: 77
Reputation: EvilLinux is an unknown quantity at this point 
Solved Threads: 0
EvilLinux EvilLinux is offline Offline
Junior Poster in Training

C# Test Score Data

 
0
  #1
Sep 10th, 2009
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,207
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: C# Test Score Data

 
0
  #2
Sep 10th, 2009
What version of visual studio and .NET framework are you using?
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,925
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: C# Test Score Data

 
0
  #3
Sep 10th, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,207
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: C# Test Score Data

 
0
  #4
Sep 10th, 2009
Here is the way I would do it with the 3.5 framework and LINQ:
  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, 6 views)
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 77
Reputation: EvilLinux is an unknown quantity at this point 
Solved Threads: 0
EvilLinux EvilLinux is offline Offline
Junior Poster in Training

Re: C# Test Score Data

 
0
  #5
Sep 10th, 2009
Originally Posted by ddanbe View Post
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?

Originally Posted by sknake View Post
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.
Last edited by EvilLinux; Sep 10th, 2009 at 3:55 pm.
Attached Thumbnails
form.png  
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,925
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: C# Test Score Data

 
1
  #6
Sep 10th, 2009
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, 7 views)
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 77
Reputation: EvilLinux is an unknown quantity at this point 
Solved Threads: 0
EvilLinux EvilLinux is offline Offline
Junior Poster in Training

Re: C# Test Score Data

 
0
  #7
Sep 10th, 2009
Originally Posted by ddanbe View Post
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!
Reply With Quote Quick reply to this message  
Reply

Tags
array, c#, data, programming

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC