Thread: payroll program with loops

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    payroll program with loops

    I deleted my last thread because I think it gave the impression that I was asking you to do my homework. I understand what i have to do, but I have one question. I am making a program that uses loops to output a list of employees, their employee number, pay, overtime etc. now this is what I have so far. I know that I can finish the prompt and output of the parameters, but I also need to give a total of all the employee's information at the end.
    Code:
    for( ; Workers!=0 ; Workers--)
    {
    cout << "\nEnter the employees first and last name: ";
    cin >> EmployeeFirstName >> EmployeeLastName;
    cout << "Enter the Employee's work Number:";
    cin >> EmployeeNumber;
    cout << "Enter the number of Dependents: ";
    cin >> Dependents;
    
    }
    basically I prompt the user to enter the number of workers before the loop starts, then it will test the loop to see if it is not equal to zero and decrements the loop each time it is excecuted. the loop just asks for the information and outputs it. I replaced outfile with cin to test it. So far it is working, just need to finish it. What I don't know how to do is take the totals of the pay, overtime, and gross etc. and add them up to the output file. We are not covering objects yet so I am not sure if there is a way to get a total out of the loop.

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    You can total it up while you're in the loop. Or you can do it outside the loop afterwards before you output. Just make sure you grab all the information first before you total them up.

    You're right you'll need objects if you want to store all the employee info. If you're just outputting the information, then you can do it all inside the loop without using objects, but this method is not very flexible and probably is not recommended.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Is there a way to create variables for the total amounts of these. for example, I create a total identifyer for each item then at the end of the loop, I do something like
    Code:
    for ( ; Workers!=0 ; Workers--)
    {....
    
    
    Total_Tax +=Total_Tax;
    Total_Overtime_Pay +=Total_Overtime_Pay;
    //etc.
    }
    
    outFile << "Total: " << setw(6) << right << Total_Tax;
                << setw(6) << right << Total_Overtime_Pay;
                //etc....
    I just have to declare total tax at the end of each users imput. Does that make sense.

  4. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    You can use an array of classes or structs to loop through and get/display the information.

    Code:
    typedef struct
    {
       char EmployeeFirstName[LENGTH];
       char EmployeeLastName[LENGTH];
       int EmployeeNumber;
       int Dependents;
       float Pay;
       float Overtime;
       float Gross;
       etc....
    }EMPLOYEE;
    
    EMPLOYEE EmployeeInfo[NUM_EMPLOYEES];
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    well, we are just starting the class and I know nothing of arrays. Would My Idea work?

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Thanks I got it now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. Program is slow. Help
    By jjj93421 in forum C++ Programming
    Replies: 14
    Last Post: 08-08-2004, 07:39 AM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  4. simple program on loops
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-11-2002, 10:24 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM