Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 460273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:51:25+00:00 2026-05-12T22:51:25+00:00

This is a homework problem – so I would appreciate if you could tell

  • 0

This is a homework problem – so I would appreciate if you could tell me what I am doing wrong and how to fix it, resp. how to optimize my programing techniques :). Thanks!

I built this to save the employee in the array – it will save the first one right but when it saves the others it shows up blank – right out of the constructor. Why?

    using System;
    using System.IO;

    class Driver
    {
        const int NUMBER_OF_EMPLOYEES = 10;

        // Main Method
        // Purpose: directs the program in what to do and loops the program while np.again != n
        // Parameters: none
        // Returns: nothing
        // Pre-conditions: none
        // Post-conditions: none
        public static void Main()
        {
            Driver nd = new Driver();
            char response = 'y';
            string PathName = "empdata" + ".txt";
            int counter = 0;

            do
            {
                employee[] emps = new employee[NUMBER_OF_EMPLOYEES];
                TextReader tr = new StreamReader(PathName);

                do
                {
                    emps[counter] = nd.getSaveEmpdataPrint(PathName, counter, tr );
                    counter++;
                } while (counter != 6);

                response = nd.again();
                Console.Clear();

            } while (response != 'n');
        }

        public employee getSaveEmpdataPrint(string PathName, int counter, TextReader tr)
        {

            employee tempEmployee = new employee();

            Driver nd = new Driver();
            double i = 1;
            string temp = "";

            do
            {
                temp = tr.ReadLine();
                if ((i % 1) == 0.5)
                    i += 0.5;

                try
                {
                if (temp != null)
                {
                    try
                    {
                        if ((int.Parse(temp)) == 1)
                        {
                            tempEmployee.empNumber = (int)(i - 0.5);
                            i += 0.5;
                        }
                    }
                    catch (FormatException){ }

                    if (i == 2)
                    {
                        string fn = "";
                        string ln = "";

                        nd.SplitString(temp, ref fn , ref ln);

                        tempEmployee.firstName = fn;
                        tempEmployee.lastName = ln;
                        i += 0.5;
                    }

                    if (i == 3)
                    {
                        tempEmployee.adress = temp;
                        i += 0.5;
                    }

                    if (i == 4)
                    {
                        double temphrwage = 0;
                        double temphrsworked = 0;
                        nd.SplitDouble(temp, ref temphrwage, ref temphrsworked);

                        tempEmployee.hrsWorked = temphrsworked;
                        tempEmployee.hrlyWage = temphrwage;
                        i += 0.5;
                    }



                }           
                }
                catch (NullReferenceException)
                    {
                        Console.WriteLine("The data in the text file was imcomplete or was formated wrong.");
                        Console.ReadLine();

                    }
            } while (temp != null && i != 4.5);

            return tempEmployee;
        }

        private void PrintEmployee(employee tempEmployee)
        {
            Console.WriteLine("-------------------------------------");
            Console.WriteLine("Employee Number: --- {0}", tempEmployee.empNumber);
            Console.WriteLine("           Name: --- {0}, {1}", tempEmployee.lastName, tempEmployee.firstName);
            Console.WriteLine("         Adress: --- {0}", tempEmployee.adress);
            Console.WriteLine("    Hourly wage: --- {0:f2} (USD per hour)", tempEmployee.hrlyWage);
            if (tempEmployee.hrsWorked == 1)
                Console.WriteLine("   Hours Worked: --- 1hr ");
            if (tempEmployee.hrsWorked != 1)
                Console.WriteLine("   Hours Worked: --- {0:f2}hrs", tempEmployee.hrsWorked);
            Console.WriteLine("-------------------------------------");
        }

        private void SplitString(string temp, ref string FirstName, ref string LastName)
        {
            char[] delimit = new char[] { ' ' };
            int counter = 0;

            foreach (string substr in temp.Split(delimit))
            {
                if (counter == 0)
                    FirstName = substr;
                if (counter == 1)
                    LastName = substr;
                counter++;
            }

        }

        private void SplitDouble(string temp, ref Double a, ref Double b)
        {
            char[] delimit = new char[] { ' ' };
            int counter = 0;

            string ta = "";
            string tb = "";

            foreach (string substr in temp.Split(delimit))
            {
                if (counter == 0)
                    ta = substr;
                if (counter == 1)
                    tb = substr;
                counter++;
            }

            a = double.Parse(ta);
            b = double.Parse(tb);

        }


        // again Method
        // Purpose: asks the user if they want to run the program again
        // Parameters: none
        // Returns: a char ( y or n )
        // Pre-conditions: none
        // Post-conditions: none
        public char again()
        {
            char response = 'y';
            Console.Write("\nWould you like run again? (y or n)");
            response = char.Parse(Console.ReadLine());
            response = char.ToLower(response);
            Console.Clear();
            return response;
        }
    }

    class employee
    {
        private int EmpNumber;
        private string FirstName;
        private string LastName;
        private string Adress;
        private double HrlyWage;
        private double HrsWorked;

        // Default Constructor
        public employee()
        {
            EmpNumber = 0;
            FirstName = "";
            LastName = "";
            Adress = "";
            HrlyWage = 0;
            HrsWorked = 0;
        }

        // Method empNumber
        // Porpose: get and set the EmpNumber
        // Pramereters: int
        // Returns: a int (employee Number)
        public int empNumber
        {
            get
            {
                return EmpNumber;
            }
            set
            {
                EmpNumber = value;
            }
        }

        // Method lastName
        // Porpose: get and set the name
        // Pramereters: string
        // Returns: a string (employee name)
        public string lastName

        {
            get
            {
                return LastName;
            }
            set
            {
                LastName = value;
            }
        }

        // Method firstName
        // Porpose: get and set the first name
        // Pramereters: string
        // Returns: a string (employee's first name)
        public string firstName
        {
            get
            {
                return FirstName;
            }
            set
            {
                FirstName = value;
            }
        }

        // Method adress
        // Porpose: get and set the adress of the employee
        // Pramereters: string
        // Returns: a string (employee adress)
        public string adress
        {
            get
            {
                return Adress;
            }
            set
            {
                Adress = value;
            }
        }

        // Method hrlyWage
        // Porpose: get and set the Hourly Wage
        // Pramereters: Double
        // Returns: a double (employee's Hourly Wage)
        public double hrlyWage
        {
            get
            {
                return HrlyWage;
            }
            set
            {
                HrlyWage = value;
            }
        }

        // Method hrsWorked
        // Porpose: get and set the Hours Worked
        // Pramereters: Double
        // Returns: a double (the number of hours the employee worked)
        public double hrsWorked
        {
            get
            {
                return HrsWorked;
            }
            set
            {
                HrsWorked = value;
            }
        }

        // Method reset
        // Porpose: reset everything to zero/default
        // Pramereters: none
        // Returns: nothing
        public void reset()
        {
            EmpNumber = 0;
            FirstName = "";
            LastName = "";
            Adress = "";
            HrlyWage = 0;
            HrsWorked = 0;
        }

    // Method empNumber
    // Porpose: get and set the EmpNumber
    // Pramereters: int
    // Returns: a int (employee Number)
    public int empNumber
    {
        get
        {
            return EmpNumber;
        }
        set
        {
            EmpNumber = value;
        }
    }

    // Method lastName
    // Porpose: get and set the name
    // Pramereters: string
    // Returns: a string (employee name)
    public string lastName

    {
        get
        {
            return LastName;
        }
        set
        {
            LastName = value;
        }
    }

    // Method firstName
    // Porpose: get and set the first name
    // Pramereters: string
    // Returns: a string (employee's first name)
    public string firstName
    {
        get
        {
            return FirstName;
        }
        set
        {
            FirstName = value;
        }
    }

    // Method adress
    // Porpose: get and set the adress of the employee
    // Pramereters: string
    // Returns: a string (employee adress)
    public string adress
    {
        get
        {
            return Adress;
        }
        set
        {
            Adress = value;
        }
    }

    // Method hrlyWage
    // Porpose: get and set the Hourly Wage
    // Pramereters: Double
    // Returns: a double (employee's Hourly Wage)
    public double hrlyWage
    {
        get
        {
            return HrlyWage;
        }
        set
        {
            HrlyWage = value;
        }
    }

    // Method hrsWorked
    // Porpose: get and set the Hours Worked
    // Pramereters: Double
    // Returns: a double (the number of hours the employee worked)
    public double hrsWorked
    {
        get
        {
            return HrsWorked;
        }
        set
        {
            HrsWorked = value;
        }
    }

    // Method reset
    // Porpose: reset everything to zero/default
    // Pramereters: none
    // Returns: nothing
    public void reset()
    {
        EmpNumber = 0;
        FirstName = "";
        LastName = "";
        Adress = "";
        HrlyWage = 0;
        HrsWorked = 0;
    }

}

}

sample txt file format:

1

John MerryWeather

123 West Main Street

5.00 30

2

Andrew Buttons

17 East Riverview Drive

12.00 40

3

Martha Washington

1 Mount Vernon Lane

7.25 20

…

Example from txt

Thanks, guys, and sorry about not having all the code 😛 and the txt file. :))

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:51:26+00:00Added an answer on May 12, 2026 at 10:51 pm

    Here is a rewritten version that should be somewhat clearer. I commented it about some things.

    My main complaint about your original code was that you made too much use of loops and counters when you didn’t have to use them. It unnecessarily complicates the code which is never a good thing.

    class Driver
    {
        // Purpose: directs the program in what to do and loops the program while np.again != n
        public static void Main()
        {
            //No need to split this into two strings just to concatenate them together immediately
            string pathName = "empdata.txt";
    
            do
            {
                //Use a list instead of an array to allow for a variable amount of employees
                List<Employee> employees = new List<Employee>();
                TextReader tr = new StreamReader(pathName);
    
                //This while statement is a common pattern for reading data from a stream.
                //Reademployee was changed to return null when no more employees are present.
                Employee employee;
                while ((employee = Employee.Read(tr)) != null)
                {
                    employees.Add(employee);
                }
    
            } while (again() != 'n');
        }
    
        // Make the method static if it doesn't have any relation to any specific object
        // Purpose: asks the user if they want to run the program again
        // Returns: a char ( y or n )
        public static char again()
        {
            char response = ' ';
            while (response != 'y' && response != 'n')
            {
                Console.Write("\nWould you like run again? (y or n)");
                response = char.Parse(Console.ReadLine());
                response = char.ToLower(response);
                Console.Clear();
            }
            return response;
        }
    }
    
    class Employee
    {
        //This is a nice syntax if using c# 3.5. It allows for simple properties with minimal code.
        public int EmployeeNumber { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Adress { get; set; }
        public double HourlyWage { get; set; }
        public int HoursWorked { get; set; }
    
        public Employee()
        {
            //Avoid code duplication by calling the reset method
            Reset();
        }
    
        public void Reset()
        {
            EmployeeNumber = 0;
            FirstName = "";
            LastName = "";
            Adress = "";
            HourlyWage = 0;
            HoursWorked = 0;
        }
    
        //This method fits better directly inside the employee class
        public static Employee Read(TextReader tr)
        {
            Employee employee = new Employee();
    
            string line = tr.ReadLine();
            //We exit as soon as we detect that the file has ended.
            //This makes the code cleaner than having nested if-else.
            //(I personally don't use the {} on single lines, but many do.
            if (line == null) {return null;} //No more posts
    
            //The whole do-while loop was removed. Unnescessary while loops should always
            //be avoided as they are one of the most difficult code constructs to follow,
    
            employee.EmployeeNumber = int.Parse(line);
    
            //Your helpmethod just complicated things. It also contained an unnesscesary loop
            string[] splitName = tr.ReadLine().Split(' ');
            employee.FirstName = splitName[0];
            employee.LastName = splitName[1];
    
            employee.Adress = tr.ReadLine();
    
            //Same as above. Also, changed HoursWorked to integer.
            string[] splitHours = tr.ReadLine().Split(' ');
            //InvariantCulture should always be used when dealing with data of a well defined format.
            //Otherwise the code won't work on computers with different culture settings.
            //(Like ones that use "," instead of ".")
            employee.HourlyWage = double.Parse(splitHours[0], System.Globalization.CultureInfo.InvariantCulture);
            employee.HoursWorked = int.Parse(splitHours[1]);
    
            return employee;
        }
    
    
        public void Print()
        {
            Console.WriteLine("-------------------------------------");
            Console.WriteLine(this.ToString());
            Console.WriteLine("-------------------------------------");
        }
    
        public override string ToString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            sb.AppendFormat("Employee Number: --- {0}", EmployeeNumber);
            sb.AppendFormat("           Name: --- {0}, {1}", LastName, FirstName);
            sb.AppendFormat("         Adress: --- {0}", Adress);
            sb.AppendFormat("    Hourly wage: --- {0:f2} (USD per hour)", HourlyWage);
            if (HoursWorked == 1)
                sb.AppendFormat("   Hours Worked: --- 1hr ");
            else
                sb.AppendFormat("   Hours Worked: --- {0:f2}hrs", HoursWorked);
            return sb.ToString();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 261k
  • Answers 261k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Resetting the state of one branch to be exactly the… May 13, 2026 at 11:42 am
  • Editorial Team
    Editorial Team added an answer For our Operating Systems course we're currently using a toy… May 13, 2026 at 11:42 am
  • Editorial Team
    Editorial Team added an answer The idea is to ask, "how many players rank above… May 13, 2026 at 11:42 am

Related Questions

This is a homework problem that I have. I have been doing some research
This is a homework problem - so I would appreciate if you could tell
After searching around a bit and consulting the Dinosaur Book, I've come to SO
This is a homework question I am sorry but I am lost. What happens
Okay this is a homework question, and I just don't have a clue how

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.