Code:
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace Chapter5Problem8
{
class Program
{
const string INPUT_FILE = "\\Projects\\Chapter5Problem8\\Chapter5Problem8\\Update.txt";
const string OUTPUT_FILE = "\\Projects\\Chapter5Problem8\\Chapter5Problem8\\output.txt";
static uint employees = 7;
static double totalPay = 0;
static int[] num = new int[employees + 1];
static string[] name = new string[employees + 1];
static double[] hourly = new double[employees + 1];
static double[] regular = new double[employees + 1];
static double[] overtime = new double[employees + 1];
static double[] regPay = new double[employees + 1];
static double[] overTimePay = new double[employees + 1];
static double[] total = new double[employees + 1];
static StreamReader fileIn;
static StreamWriter fileOut;
static void Main()
{
if (OpenFiles())
{
InputData();
Total();
Display();
CloseFiles();
}
}
static bool OpenFiles()
{
bool successOpeningFile = true;
if (File.Exists(INPUT_FILE))
{
fileIn = File.OpenText(INPUT_FILE);
Console.WriteLine("{0} was opened!", INPUT_FILE);
}
else
{
Console.WriteLine("Error: {0} does not exist!", INPUT_FILE);
successOpeningFile = false;
}
fileOut = File.CreateText(OUTPUT_FILE);
if (File.Exists(OUTPUT_FILE))
Console.WriteLine("{0} was created!", OUTPUT_FILE);
else
{
Console.WriteLine("Error: {0} could not be created!", OUTPUT_FILE);
successOpeningFile = false;
}
return successOpeningFile;
}
static void InputData()
{
uint i = 0;
string lineIn;
while ((lineIn = fileIn.ReadLine()) != null)
{
ParseLineIn(lineIn, i);
i++;
}
}
static void ParseLineIn(string lineIn, uint i)
{
string[] words = new string[2];
lineIn = lineIn.Trim();
while (Regex.IsMatch(lineIn, "[ ] {2}"))
lineIn = lineIn.Replace(" ", " ");
words = lineIn.Split(' ');
num[i] = int.Parse(words[0]);
name[i] = words[1];
hourly[i] = double.Parse(words[2]);
regular[i] = double.Parse(words[3]);
overtime[i] = double.Parse(words[4]);
}
static void Total()
{
int i;
for (i = 0; i <= employees; i++)
{
regPay[i] = regular[i] * hourly[i];
overTimePay[i] = overtime[i] * hourly[i] * 1.5F;
total[i] = (double) Math.Round(regPay[i] + overTimePay[i],2);
totalPay += total[i];
}
}
static void Display()
{
int i;
fileOut.WriteLine("# Name Reg Hours Overtime Rate total");
fileOut.WriteLine("------------------------------------------------------------------------------");
for (i = 0; i <= employees - 1; i++)
{
fileOut.WriteLine("{0} {1,-8} {2,-10} {3,-10} {4,-6:C} {5:C}", num[i], name[i], regular[i], overtime[i], hourly[i], total[i]);
}
fileOut.WriteLine("-------------------------------------------------------------------------------");
fileOut.WriteLine(" {0:C}",totalPay);
}
static void CloseFiles()
{
fileIn.Close();
fileOut.Close();
}
}
}
Here's code from my other program that works
here's the text
1 Smith 7.50 38.41 0
2 Scoggins 11.00 40.00 5.33
3 Gray 5.00 33.87 0
4 Ensor 9.50 40.00 22.3
5 Pinet 22.00 38.58 0
6 Jones 4.55 28.00 0
7 Chance 15.33 40.00 4.44
here's the output
output.txt