Here is the problem:
Write a C# program that will read five scores, each on a separate line, from an external file named Unit09data.txt. Your program should then calculate the average of those scores and write the average, formatted to two decimal places in a different external file named output.txt. Be sure that your program handles IOExceptions.
The result should be 3.0 and create and write to another txt file outout.txt.
The exceptions seem to be working but it is not creating and writing to the output.txt file so I am not even sure if it is reading and computing this correctly.
Can anyone help?
Here is what I have..
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace Unit09Project
{
public class frmExceptions : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnOpen;
private System.Windows.Forms.Label lblOutput;
private StreamReader iFile;
private StreamWriter oFile;
private System.ComponentModel.Container components = null;
public frmExceptions()
{
InitializeComponent();
}
/// Clean up any resources being used.
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnOpen = new System.Windows.Forms.Button();
this.lblOutput = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnOpen
//
this.btnOpen.Location = new System.Drawing.Point(100, 65);
this.btnOpen.Name = "btnOpen";
this.btnOpen.TabIndex = 0;
this.btnOpen.Text = "Open File";
this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);
//
// lblOutput
//
this.lblOutput.Location = new System.Drawing.Point(20, 150);
this.lblOutput.Name = "lblOutput";
this.lblOutput.Size = new System.Drawing.Size(170, 23);
this.lblOutput.TabIndex = 1;
//
// frmExceptions
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(284, 244);
this.Controls.Add(this.lblOutput);
this.Controls.Add(this.btnOpen);
this.Name = "frmExceptions";
this.Text = "Unit 09 Project";
this.Load += new System.EventHandler(this.frmExceptions_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmExceptions());
}
private void frmExceptions_Load(object sender, System.EventArgs e)
{
}
private void btnOpen_Click(object sender, System.EventArgs e)
{
int totalScore = 0;
int scoreCount = 0;
double averageTestScore;
StreamReader iFile = new StreamReader(@"C:\Users\fjones\Documents\Visual Studio Projects\Unit09Project\bin\Debug\Unit09data.txt.txt");
if(File.Exists(@"C:\Users\fjones\Documents\Visual Studio Projects\Unit09Project\bin\Debug\Unit09data.txt.txt"))
try
{
totalScore += Convert.ToInt32(iFile.ReadLine());
scoreCount++;
averageTestScore = totalScore/scoreCount;
StreamWriter oFile = new StreamWriter(@"C:\Users\fjones\Documents\Visual Studio Projects\bin\Debug\output.txt");
oFile.Write(String.Format("The average of these scores is: {0}", averageTestScore));
}
catch (System.IO.IOException exc)
{
lblOutput.Text = exc.Message;
}
finally
{
iFile.Close();
}
lblOutput.Text = "Progress made";
}
}
}