0

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.

yinnonsanders
  • 1,831
  • 11
  • 28
  • Where is sum declared? You have to assign it a value as floats and ints are not null able by default. – Sam Marion Nov 13 '17 at 21:08
  • You see how you declare your ints with values = 0? You need to do the same thing with your floats. – Sam Marion Nov 13 '17 at 21:10
  • You should really be using the `using` keyword/statement to dispose of your `IDisposable`... like your `StreamReader`. – Erik Philips Nov 13 '17 at 21:11
  • read up on how to initialize local variables.. and edit your code and put your variable declarations in the same place – MethodMan Nov 13 '17 at 21:12

2 Answers2

3

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;
    }
}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
1

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.

Dave Smash
  • 2,941
  • 1
  • 18
  • 38
  • It's probably better to cast the int direclty to a float than it is to convert it to a string and then to a float. – Sam I am says Reinstate Monica Nov 13 '17 at 21:21
  • @SamIam - true, answer was updated accordingly. He could also just declare total and count as floats and not bother with the integers at all if this is all they are used for... – Dave Smash Nov 13 '17 at 21:26
  • It looks like I was able to successfully run my program by simply adding the (float) in front of total and count. Looking through it now it is easy to see where I messed up so thank you all. (I know it says not to use the comments to say thank you so sorry, but I feel bad for not saying anything.) So it went from this sum = float.Parse(textLine); length = float.Parse(textLine); average = sum / length; to float average = (float)total / (float)count; – Thatonepeep Nov 13 '17 at 21:32