Reputation: 23
I keep getting the aggravated CS0165 error code. I have re-written this several times and even googled the issues. I cannot resolve this, it is for a college assignment that is now 2 days late. Can anyone please help. Would appreciate. This is the code below:
using System;
using System.IO;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
namespace Assignment_2
{
class Program
{
static void Main()
{
string InputPath = @"C:\Users\....\Desktop\CP Class\Assignment 2\Asgn2InputFile.txt";
string OutputPath = @"C:\Users\....\Desktop\CP Class\Assignment 2\Payroll.txt";
using (StreamReader sr = new StreamReader(InputPath))
using (StreamWriter sw = new StreamWriter(OutputPath))
{
// input line
string InputLine;
// input fields
string First;
string Last;
int Hours;
int OverT;
double Pay;
// output fields
double Earnings;
double Gross;
//
sw.Write("NAME".PadRight(11));
sw.Write("HOURS WORKED ".PadRight(23));
sw.Write("PAY RATE".PadRight(20));
sw.Write("OVERTIME".PadRight(27));
sw.WriteLine();
while ((InputLine = sr.ReadLine()) != null)
// parse input line
First = InputLine.Substring(0, 5);
Last = InputLine.Substring(0, 11);
Pay = double.Parse(InputLine.Substring(31, 5));
Hours = int.Parse(InputLine.Substring(16, 2));
OverT = int.Parse(InputLine.Substring(29, 1));
//
Earnings = (Hours * Pay);
Gross = Earnings + (OverT * (Pay * 1.5));
sw.Write(First.PadRight(11)); [[ Error Code occurs here ]]
sw.WriteLine(Last.PadRight(11));
//
sw.Write(Earnings.ToString().PadLeft(10) + " @ " + Gross.ToString("C").PadRight(9));
sw.WriteLine(Earnings.ToString("C").PadLeft(17));
sw.WriteLine();
//Total += Earnings;
}
}
}
}
Upvotes: 0
Views: 117
Reputation: 4394
You have multiple issues in your code
Look at the below code which I think is what you want
using System;
using System.IO;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
namespace Assignment_2 {
class Program {
static void Main() {
string InputPath = @"C:\Users\....\Desktop\CP Class\Assignment 2\Asgn2InputFile.txt";
string OutputPath = @"C:\Users\....\Desktop\CP Class\Assignment 2\Payroll.txt";
using (StreamReader sr = new StreamReader(InputPath))
using (StreamWriter sw = new StreamWriter(OutputPath)) {
sw.Write("NAME".PadRight(11));
sw.Write("HOURS WORKED ".PadRight(23));
sw.Write("PAY RATE".PadRight(20));
sw.Write("OVERTIME".PadRight(27));
sw.WriteLine();
// input line
string InputLine;
while ((InputLine = sr.ReadLine()) != null) {
// parse input line
var First = InputLine.Substring(0, 5);
var Last = InputLine.Substring(0, 11);
var Pay = double.Parse(InputLine.Substring(31, 5));
var Hours = int.Parse(InputLine.Substring(16, 2));
var OverT = int.Parse(InputLine.Substring(29, 1));
//
var Earnings = (Hours * Pay);
var Gross = Earnings + (OverT * (Pay * 1.5));
sw.Write(First.PadRight(11)); //Error Code occurs here
sw.WriteLine(Last.PadRight(11));
//
sw.Write(Earnings.ToString().PadLeft(10) + " @ " + Gross.ToString("C").PadRight(9));
sw.WriteLine(Earnings.ToString("C").PadLeft(17));
sw.WriteLine();
//Total += Earnings;
}
}
}
}
}
Upvotes: 0
Reputation: 118987
Because your while
loop doesn't have any braces to block the code it contains, it only takes the first line of code beneath as the content. Because the compiler cannot guarantee that line will run, the variable First
might not be initialised.
So instead of doing this:
while (something)
DoSomething();
DoSomethingElse();
You should write:
while (something)
{
DoSomething();
DoSomethingElse();
}
Upvotes: 1
Reputation: 48278
The error CS0165 is because you are using variables that are not initialized....
the reason is the omitting of the { } here
while ((InputLine = sr.ReadLine()) != null)
which means, the while loop has as scope something else as what you need
in fact your code is equivalent to:
while ((InputLine = sr.ReadLine()) != null){
First = InputLine.Substring(0, 5);
}
leaving the rest uninitialized
Upvotes: 1