Reputation: 17
My goal is to produce the average of the numbers a person types into the box (which I have already set up). The equation needs to be after the f.Close(); and the data type needs to be float.
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void btnOpen_Click(object sender, EventArgs e)
{
int total = 0, count = 0;
DialogResult result;
result = ofd.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
string fileName = ofd.FileName;
StreamReader f = new StreamReader(fileName);
while (!f.EndOfStream)
{
string textScore = f.ReadLine();
firstScores.Items.Add(textScore);
int score = int.Parse(textScore);
total = total + score;
count = count + 1;
}
f.Close();
//This is what I have currently
float sum, length, average;
string textLine;
sum = float.Parse(textLine);
length = float.Parse(textLine);
average = sum / length;
I thought this might work, but it states that the textLine in the sum = float.Parse is unassigned.
Upvotes: 0
Views: 712
Reputation: 3001
The average is the total divided by the count. You are already getting the total and the count as you go.
You already have a hint - they need to be floats, not integers, so cast them like this:
float floatTotal = (float)total;
float floatCount = (float)count;
then do your math:
float average = floatTotal / floatCount;
You will also want to check that you are not dividing by zero (ie: that count > 0). This will keep your program from crashing if you don't read any scores in.
Upvotes: 1
Reputation: 415665
he textLine in the sum = float.Parse is unassigned.
You have these two lines together:
string textLine;
sum = float.Parse(textLine);
You just declared textLine
, but haven't stored anything in it yet.
What you really want to do is use the data you already read in from the file. When you were reading the file, you put that data in the firstScores.Items
collection, and you already have the total
and count
calculated. You just need to use those same values, and make sure to cast (not Parse()
) them to float
to avoid integer division, which would truncate the decimal portion (the link is written for old-school C, but the contents are true for C# as well).
BTW, you're still learning, and the professor likely has specific objectives for you in going through the process of using a StreamReader, etc. But I think it's also worthwhile to show you how I'd do this in a real program for work:
//First, separate out the this code into a function that accepts a file name and returns the values via the IEnumerable interface, rather than an array or list
IEnumerable<int> ReadScores(string filename)
{
//Use the `System.IO.File` class to make reading simpler
// and use the linq functions to get this down to a single line of code:
return File.ReadLines(filename).Select(l => int.Parse(l));
}
private void btnOpen_Click(object sender, EventArgs e)
{
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//ToList() is optional, but it will force the data into memory, so we only need to go disk once
var data = ReadScores(ofd.FileName).ToList();
int total = data.Sum();
int count = data.Count;
float average = (float)total / (float)count;
}
}
Upvotes: 3