1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Report problem

Discussion in 'C#' started by Ghadoor, Jan 20, 2010.

  1. Ghadoor

    Ghadoor New Member

    Joined:
    Jan 20, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I have a problem, and I really need your help!
    I have this project and I'm done with it .. But I have a logical error .. When it comes to the print part it prints only the last name and the last calculated average .. I couldn't figure out the error

    The program is created to enter employee name and their worked hours during the year and then calculating their averages ,. And at the end it should print a report with the name I entered and the averages ..

    The program goes this way:
    Pleas enter employee name: hfgjd
    Please enter the worked hours: 2
    Please enter worked hours:2
    please enter worked hours: 0
    The average is:2
    Would you like to continue? (Y/N) .. N
    "The report part"

    The class:
    Code:
     [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Employee[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2]{[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] employeeName;[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][] workHours = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][12];[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]bool[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] validIndex = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] avgWorkedHours;[/SIZE]
     
     
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] getEmployeeName()[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] employeeName;[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] setEmployeeName([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] name)[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2]employeeName = name;[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] getWorkHours([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] index)[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] workHours[index];[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] setWorkHours([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] hours, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] index)[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (index < 0 || index > 12)[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Wrong Entry!"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    [SIZE=2]validIndex = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2]{[/SIZE]
    [SIZE=2]workHours[index] = hours;[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]bool[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] getValidIndex()[/SIZE]
    [SIZE=2]{[/SIZE]
     
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] validIndex;[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] getAvgWorkedHours()[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] avgWorkedHours;[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] setAvgWorkedHours([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] average)[/SIZE]
    [SIZE=2]{[/SIZE]
     
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] max = 0;[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sum = 0;[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i = 0; i < 12; i++)[/SIZE]
    [SIZE=2]{[/SIZE]
     
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (workHours[i] == 0)[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2]max = i;[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]break[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2]{[/SIZE]
    [SIZE=2]sum += workHours[i];[/SIZE]
    [SIZE=2]max = i;[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (max > 0)[/SIZE]
    [SIZE=2]average = sum / max;[/SIZE]
    [SIZE=2]avgWorkedHours = average;[/SIZE]
     
    [SIZE=2]}[/SIZE]
    [SIZE=2]}[/SIZE]
     
     
  2. Ghadoor

    Ghadoor New Member

    Joined:
    Jan 20, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    The program:

    Code:
     classProgram
    
    {
    
    staticvoid Main(string[] args)
    
    {
    
     
    
    Employee[] employeeInfo = newEmployee[25];
    
    for (int i = 0; i < employeeInfo.Length; i++)
    
    employeeInfo[i] = newEmployee();
    
     
    
    int employeeCount = 0;
    
    // Puting an Array into a Class:
    
    try
    
    {
    
     
    
    Console.WriteLine("Welcome To The Program!");
    
    Console.WriteLine("Please inform, that you have to enter a 0, after entering the whole worked hours ..!");
    
    string input = "";
    
    do
    
    { 
    
    int counter = 0; 
    
    int hours = 0;
    
    // To enter the employee information:
    
    
    Console.Write("Please Enter The Employee Name: ");
    
     
    
    employeeInfo[counter].setEmployeeName(Console.ReadLine());
    
    int hourCounter = 0;
    
    do
    
    { 
    
    Console.Write("Please Enter The Employee Worked Hours For The Month:");
    
    hours = (Int32.Parse(Console.ReadLine()));
    
    if (hours == 0)
    
    break;
    
    employeeInfo[counter].setWorkHours(hours, hourCounter);
    
    hourCounter++;
    
    } while (hourCounter < 12);
    
    double avg = 0;
    
    employeeInfo[counter].setAvgWorkedHours(avg);
    
    Console.WriteLine("The Average For The Employee Worked Hours is : " + employeeInfo[counter].getAvgWorkedHours());
    
    Console.Write("Would you like to continue ? (Y/N) .. ");
    
    input = Console.ReadLine();
    
    Console.WriteLine("_______________________________________");
    
    employeeCount++;
    
    } while (employeeCount < 25 && input == "Y" || input == "y");
    
    Console.Clear();
    
     
    
    // To print the report:
    
    for (int n = 0; n < employeeInfo.Length; n++)
    
    {
    
    Console.WriteLine("The Employee Name is: {0} and His Average for the Worked Hours is: {1} ", employeeInfo[n].getEmployeeName(), employeeInfo[n].getAvgWorkedHours());
    
    }
    
     
    
    }
    
     
    
    // To catch the errors while running the program:
    
    catch (DivideByZeroException expOne)
    
    {
    
    Console.WriteLine("An Error Occurred " + expOne.Message);
    
    }
    
    catch (FormatException expTwo)
    
    {
    
    Console.WriteLine("An Error Occurred " + expTwo.Message);
    
    }
    
    finally
    
    {
    
    Console.WriteLine("Thank You!");
    
    }
    
     
    
     
    
    Console.ReadLine();
    
     
    
     
     

Share This Page