Here's the issue; I'm trying to readline's from a .txt, however there are blank lines inbetween each number, so it's throwing me an error and filling the rest of my array with the default 0's. should I be using trim before parsing?
(this was on a c# final, and my instructor may or may not go over the final answer so I am asking here out of curiosity, my solution was to edit the .txt file so there were no empty lines. lol.)
private void getScoreButton_Click(object sender, EventArgs e)
{
double sum = 0.00;
double avg = 0.00;
int numBelowAvg = 0;
int numAboveAvg = 0;
const int SIZE = 20;
StreamReader inputFilesData;
int i = 0;
int[] scores = new int[SIZE];
try
{
inputFilesData = File.OpenText("scores.txt");
while (i < scores.Length && !inputFilesData.EndOfStream)
{
scores[i] = int.Parse(inputFilesData.ReadLine());
i++;
}
inputFilesData.Close();
}
catch (Exception excep)
{
MessageBox.Show(excep.Message);
}
for(int j= 0; j <scores.Length; j++)
{
scoreListBox.Items.Add(scores[j]);
sum += scores[j];
}
avg = sum / scores.Length;
for (int j = 0; j < scores.Length; j++)
{
if(scores[j]>avg)
{
numAboveAvg++;
}
else
{
numBelowAvg++;
}
}
avgLabel.Text = avg.ToString("N2");
aboveAvgLabel.Text = numAboveAvg.ToString();
belowAvgLabel.Text = numBelowAvg.ToString();
}
}
}