here is my code so far. Im pulling data from a txt file and writing to a new file with averages of Age and experience. I'm not getting the right averages. Where is my mistake?
class Program
{
static int age, exp, id, ageSum = 0, expSum = 0, empSum = 0;
static double avgAge, avgExp;
static char type;
const string INPUT_FILE_NAME =
"\\Users\\Jon.Jon-PC\\Documents\\Visual Studio 2015\\Projects\\ConsoleApplication9\\ConsoleApplication9\\data.txt";
const string OUTPUT_FILE_NAME =
"\\Users\\Jon.Jon-PC\\Documents\\Visual Studio 2015\\Projects\\ConsoleApplication9\\ConsoleApplication9\\dataout.txt";
static StreamReader fileIn;
static StreamWriter fileOut;
static string lineIn, eligibility;
static void OpenFiles()
{
if (File.Exists(INPUT_FILE_NAME))
{
fileIn = File.OpenText(INPUT_FILE_NAME);
Console.WriteLine("{0} was opened", INPUT_FILE_NAME);
}
else
{
Console.WriteLine("Error: {0} does not exit\n",
INPUT_FILE_NAME);
Environment.Exit(0);
}
fileOut = File.CreateText(OUTPUT_FILE_NAME);
if (File.Exists(OUTPUT_FILE_NAME))
Console.WriteLine("{0} was created\n",
OUTPUT_FILE_NAME);
else
{
Console.WriteLine("Error: {0} could not be created\n",
OUTPUT_FILE_NAME);
Environment.Exit(0);
}
}
static void ParseLineIn()
{
string[] words = new string[4];
lineIn = lineIn.Trim();
while (Regex.IsMatch(lineIn, "[ ]{2}"))
lineIn = lineIn.Replace(" ", " ");
words = lineIn.Split(' ');
id = int.Parse(words[0]);
type = char.Parse(words[1]);
age = int.Parse(words[2]);
exp = int.Parse(words[3]);
}
static void UpdateTotals()
{
empSum++;
ageSum += age;
expSum += exp;
}
static void CalcAvg()
{
avgAge = ageSum / empSum;
avgExp = expSum / empSum;
}
static void PrintAvg()
{
fileOut.Write("Avg\t {0} {1}", avgAge, avgExp);
}
static void CalcRetirement()
{
switch (type)
{
case 'm':
case 'M':
if (age < 55 && exp < 20)
eligibility = ("lack of experience age");
else if (age >= 55 && exp >= 20)
eligibility = ("can retire");
else if (age >= 55 && exp < 20)
eligibility = ("lack of experience");
else if (age < 55 && exp >= 20)
eligibility = ("underage");
else
eligibility = ("Your entry is invalid");
break;
case 'w':
case 'W':
if (age < 63 && exp < 25)
eligibility = ("lack of exp age");
else if (age >= 63 && exp >= 25)
eligibility = ("can retire");
else if (age >= 63 && exp < 25)
eligibility = ("lack of exp");
else if (age < 63 && exp >= 25)
eligibility = ("lack age");
else
eligibility = ("Your entry is invalid");
break;
case 's':
case 'S':
if (age < 60 && exp < 24)
eligibility = ("lack of exp age");
else if (age >= 60 && exp >= 24)
eligibility = ("can retire");
else if (age >= 60 && exp < 24)
eligibility = ("lack of exp");
else if (age < 60 && exp >= 24)
eligibility = ("underage");
else
eligibility = ("Your entry is invalid");
break;
}
}
static void CloseFiles()
{
fileIn.Close(); fileOut.Close();
}
static void PrintReportHeadings()
{
fileOut.WriteLine(" Employee Report ");
fileOut.WriteLine();
fileOut.WriteLine(" ID Age Exp Eligibility ");
fileOut.WriteLine("---- --- --- ----------- ");
}
static void printDetailLine()
{
fileOut.WriteLine("{0} {1} {2} {3}", id, age, exp, eligibility);
}
static void Main(string[] args)
{
OpenFiles();
PrintReportHeadings();
while ((lineIn = fileIn.ReadLine()) != null)
{
UpdateTotals();
ParseLineIn();
CalcRetirement();
printDetailLine();
}
CalcAvg();
PrintAvg();
CloseFiles();
}
}
I am pretty new to coding and so I don't know much else besides what I've learned this semester. The data from the file I'm pulling is below. Im supposed to calculate averages of the A and E columns.
A E
1235 W 45 20
2536 W 55 21
5894 W 60 30
4597 W 75 35
2597 S 35 10
5689 S 40 20
5489 W 55 39
5872 M 60 40
5569 M 55 25
5566 W 80 20
8865 M 59 35
5598 S 65 35
My current output is below..
Employee Report
ID Age Exp Eligibility
---- --- --- -----------
1235 45 20 lack of exp age
2536 55 21 lack of exp age
5894 60 30 lack age
4597 75 35 can retire
2597 35 10 lack of exp age
5689 40 20 lack of exp age
5489 55 39 lack age
5872 60 40 can retire
5569 55 25 can retire
5566 80 20 lack of exp
8865 59 35 can retire
5598 65 35 can retire
Avg 51 24
The averages are supposed to be 57.0 and 27.5 What am I missing?