EDN Admin
Well-known member
I am trying to pull a file into my program but it says that the file doesnt exist. It does and Im pretty sure I am referring to it correctly, but am not sure. This is for my programming I class.
using System;<br/>
using System.IO;<br/>
using LibUtil;<br/>
<br/>
// Begin namespace Ch5Prb2<br/>
namespace Ch5Prb2<br/>
{<br/>
// Begin application class Ch5Prb2<br/>
class Ch5Prb2<br/>
{<br/>
const string INPUT_FILE_NAME = @"CIS110Ch5Prb2Payroll1Dat.Txt"; <------- this is the one that is said not to exist<br/>
const string OUTPUT_FILE_NAME = @"CIS110Ch5Prb2Ch5Prb2Rpt.Txt"; <--- then I want to create a new file
<br/>
<br/>
const double FICA_RATE = 0.07;<br/>
const double FED_TAX_RATE = 0.22;<br/>
const double STATE_TAX_RATE = 0.05;<br/>
const double UNION_DUES = 6.50;<br/>
const double HEALTH_INS_SURCHARGE = 10.00;<br/>
const uint MAX_NUM_OF_DEPENDENTS = 2;<br/>
<br/>
static string lineIn;<br/>
static uint numOfDependents, id;<br/>
static double payRate, hoursWorked, basePay, ovtPay, grossPay, netPay;<br/>
static double fica, fedTax, stateTax, healthInsurance, unionDues;<br/>
<br/>
static StreamReader fileIn;<br/>
static StreamWriter fileOut;<br/>
<br/>
static void Main()<br/>
{<br/>
ConsoleApp.ClrScr();<br/>
OpenFiles();<br/>
PrintReportHeadings();<br/>
while ((lineIn = StringMethods.SpaceDelimit(fileIn.ReadLine())) != null)<br/>
{<br/>
ParseLineIn();<br/>
CalcPayroll();<br/>
PrintDetailLine();<br/>
}<br/>
CloseFiles();<br/>
}<br/>
<br/>
static void OpenFiles()<br/>
{<br/>
try<br/>
{<br/>
fileIn = File.OpenText(INPUT_FILE_NAME);<br/>
Console.WriteLine("{0} was opened", INPUT_FILE_NAME);<br/>
}<br/>
catch<br/>
{<br/>
Console.WriteLine("Error: {0} does not existn", INPUT_FILE_NAME);<br/>
ConsoleApp.Exit();<br/>
}<br/>
try<br/>
{<br/>
fileOut = File.CreateText(OUTPUT_FILE_NAME);<br/>
Console.WriteLine("{0} was createdn", OUTPUT_FILE_NAME);<br/>
}<br/>
catch<br/>
{<br/>
Console.WriteLine("Error: {0} could not be createdn", OUTPUT_FILE_NAME);<br/>
ConsoleApp.Exit();<br/>
}<br/>
}<br/>
static void ParseLineIn()<br/>
{<br/>
string[] words = lineIn.Split( );<br/>
{<br/>
id = UInt32.Parse(words[0]);<br/>
payRate = Double.Parse(words[1]);<br/>
hoursWorked = Double.Parse(words[2]);<br/>
numOfDependents = UInt32.Parse(words[3]);<br/>
}<br/>
}<br/>
<br/>
static void PrintReportHeadings()<br/>
{<br/>
fileOut.WriteLine("
Chapter 5 Problem 2 ");<br/>
fileOut.WriteLine("
Payroll Report
");<br/>
fileOut.WriteLine("
{0:MM/dd/yyyy}", DateTime.Now );<br/>
fileOut.WriteLine();<br/>
fileOut.WriteLine("
Tiffany Doughty
");<br/>
fileOut.WriteLine();<br/>
fileOut.WriteLine(" Pay Hours Base Ovt Gross
Fed State Health Union Net ");<br/>
fileOut.WriteLine(" ID Dep Rate Worked Pay Pay Pay FICA Tax Tax
Ins Dues Pay ");<br/>
fileOut.WriteLine("---- --- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- -------");<br/>
}<br/>
<br/>
static void CalcPayroll()<br/>
{<br/>
if (hoursWorked <= 40.0)<br/>
{<br/>
basePay = hoursWorked * payRate;<br/>
ovtPay = 0.00;<br/>
}<br/>
else<br/>
{<br/>
basePay = 40.0 * payRate;<br/>
ovtPay = (hoursWorked - 40.0) * payRate * 1.5;<br/>
}<br/>
grossPay = basePay + ovtPay;<br/>
fica = grossPay * FICA_RATE;<br/>
fedTax = grossPay * FED_TAX_RATE;<br/>
stateTax = grossPay * STATE_TAX_RATE;<br/>
if (numOfDependents > MAX_NUM_OF_DEPENDENTS)<br/>
healthInsurance = HEALTH_INS_SURCHARGE;<br/>
else<br/>
healthInsurance = 0.00;<br/>
netPay = grossPay - (fica + fedTax + stateTax +<br/>
UNION_DUES + healthInsurance);<br/>
unionDues = 6.50; <br/>
}<br/>
<br/>
static void PrintDetailLine()<br/>
{<br/>
fileOut.WriteLine("{0:d4} {1,3:f} {2,7:f} {3,7:f} {4,7:f} {5,7:f} {6,7:f} {7,7:f} {8,7:f}, {9,7:f} {10,7:f} {11,7:f} {12,7:f} {13,7:f}",<br/>
id, numOfDependents, payRate, hoursWorked, basePay, ovtPay, grossPay, fica, fedTax, stateTax,<br/>
healthInsurance, unionDues, netPay);<br/>
}<br/>
<br/>
static void CloseFiles()<br/>
{<br/>
fileIn.Close(); fileOut.Close();<br/>
}<br/>
} // End application class Ch5Prb2<br/>
} // End namespace Ch5Prb2
View the full article
using System;<br/>
using System.IO;<br/>
using LibUtil;<br/>
<br/>
// Begin namespace Ch5Prb2<br/>
namespace Ch5Prb2<br/>
{<br/>
// Begin application class Ch5Prb2<br/>
class Ch5Prb2<br/>
{<br/>
const string INPUT_FILE_NAME = @"CIS110Ch5Prb2Payroll1Dat.Txt"; <------- this is the one that is said not to exist<br/>
const string OUTPUT_FILE_NAME = @"CIS110Ch5Prb2Ch5Prb2Rpt.Txt"; <--- then I want to create a new file
<br/>
<br/>
const double FICA_RATE = 0.07;<br/>
const double FED_TAX_RATE = 0.22;<br/>
const double STATE_TAX_RATE = 0.05;<br/>
const double UNION_DUES = 6.50;<br/>
const double HEALTH_INS_SURCHARGE = 10.00;<br/>
const uint MAX_NUM_OF_DEPENDENTS = 2;<br/>
<br/>
static string lineIn;<br/>
static uint numOfDependents, id;<br/>
static double payRate, hoursWorked, basePay, ovtPay, grossPay, netPay;<br/>
static double fica, fedTax, stateTax, healthInsurance, unionDues;<br/>
<br/>
static StreamReader fileIn;<br/>
static StreamWriter fileOut;<br/>
<br/>
static void Main()<br/>
{<br/>
ConsoleApp.ClrScr();<br/>
OpenFiles();<br/>
PrintReportHeadings();<br/>
while ((lineIn = StringMethods.SpaceDelimit(fileIn.ReadLine())) != null)<br/>
{<br/>
ParseLineIn();<br/>
CalcPayroll();<br/>
PrintDetailLine();<br/>
}<br/>
CloseFiles();<br/>
}<br/>
<br/>
static void OpenFiles()<br/>
{<br/>
try<br/>
{<br/>
fileIn = File.OpenText(INPUT_FILE_NAME);<br/>
Console.WriteLine("{0} was opened", INPUT_FILE_NAME);<br/>
}<br/>
catch<br/>
{<br/>
Console.WriteLine("Error: {0} does not existn", INPUT_FILE_NAME);<br/>
ConsoleApp.Exit();<br/>
}<br/>
try<br/>
{<br/>
fileOut = File.CreateText(OUTPUT_FILE_NAME);<br/>
Console.WriteLine("{0} was createdn", OUTPUT_FILE_NAME);<br/>
}<br/>
catch<br/>
{<br/>
Console.WriteLine("Error: {0} could not be createdn", OUTPUT_FILE_NAME);<br/>
ConsoleApp.Exit();<br/>
}<br/>
}<br/>
static void ParseLineIn()<br/>
{<br/>
string[] words = lineIn.Split( );<br/>
{<br/>
id = UInt32.Parse(words[0]);<br/>
payRate = Double.Parse(words[1]);<br/>
hoursWorked = Double.Parse(words[2]);<br/>
numOfDependents = UInt32.Parse(words[3]);<br/>
}<br/>
}<br/>
<br/>
static void PrintReportHeadings()<br/>
{<br/>
fileOut.WriteLine("
Chapter 5 Problem 2 ");<br/>
fileOut.WriteLine("
Payroll Report
");<br/>
fileOut.WriteLine("
{0:MM/dd/yyyy}", DateTime.Now );<br/>
fileOut.WriteLine();<br/>
fileOut.WriteLine("
Tiffany Doughty
");<br/>
fileOut.WriteLine();<br/>
fileOut.WriteLine(" Pay Hours Base Ovt Gross
Fed State Health Union Net ");<br/>
fileOut.WriteLine(" ID Dep Rate Worked Pay Pay Pay FICA Tax Tax
Ins Dues Pay ");<br/>
fileOut.WriteLine("---- --- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- -------");<br/>
}<br/>
<br/>
static void CalcPayroll()<br/>
{<br/>
if (hoursWorked <= 40.0)<br/>
{<br/>
basePay = hoursWorked * payRate;<br/>
ovtPay = 0.00;<br/>
}<br/>
else<br/>
{<br/>
basePay = 40.0 * payRate;<br/>
ovtPay = (hoursWorked - 40.0) * payRate * 1.5;<br/>
}<br/>
grossPay = basePay + ovtPay;<br/>
fica = grossPay * FICA_RATE;<br/>
fedTax = grossPay * FED_TAX_RATE;<br/>
stateTax = grossPay * STATE_TAX_RATE;<br/>
if (numOfDependents > MAX_NUM_OF_DEPENDENTS)<br/>
healthInsurance = HEALTH_INS_SURCHARGE;<br/>
else<br/>
healthInsurance = 0.00;<br/>
netPay = grossPay - (fica + fedTax + stateTax +<br/>
UNION_DUES + healthInsurance);<br/>
unionDues = 6.50; <br/>
}<br/>
<br/>
static void PrintDetailLine()<br/>
{<br/>
fileOut.WriteLine("{0:d4} {1,3:f} {2,7:f} {3,7:f} {4,7:f} {5,7:f} {6,7:f} {7,7:f} {8,7:f}, {9,7:f} {10,7:f} {11,7:f} {12,7:f} {13,7:f}",<br/>
id, numOfDependents, payRate, hoursWorked, basePay, ovtPay, grossPay, fica, fedTax, stateTax,<br/>
healthInsurance, unionDues, netPay);<br/>
}<br/>
<br/>
static void CloseFiles()<br/>
{<br/>
fileIn.Close(); fileOut.Close();<br/>
}<br/>
} // End application class Ch5Prb2<br/>
} // End namespace Ch5Prb2
View the full article