Help with Nested Structures

I was wondering how is the proper way to do the calculations using a nested structure. I was asked to write a program that contained a nested structure and calculate a weekly payroll. One structure held the departments and calculations(manager, sales associate) and another structure contained ID, Hours and Rate. I then had to ask for each individual employee's ID, hours and rate in which I understood what to do. But once I got to the calculations for the gross pay, federal tax and other calculations is where I got lost. What direction do I need to go in order to calculate the 9 employees with little code?


#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

//Create Structures

struct Department //structure for pay calculations & department category
{
char dept;
double gpay;
double overtime;
double fedtax;
double oasditax;
double hitax;
double npay;
};

struct Empinfo //structure for employee id, rate & hours
{
Department d1 ;
int ID;
double rate;
double hours;
};

int main()

{

//Number of managers, salespersons and bookkeepers

Empinfo m1, m2, s1, s2, s3, s4, b1, b2, b3;


//Assigned tax values

m1.d1.oasditax = 0.062;
m2.d1.oasditax = 0.062;
s1.d1.oasditax = 0.062;
s2.d1.oasditax = 0.062;
s3.d1.oasditax = 0.062;
s4.d1.oasditax = 0.062;
b1.d1.oasditax = 0.062;
b2.d1.oasditax = 0.062;
b3.d1.oasditax = 0.062;
m1.d1.hitax = 0.0145;
m2.d1.hitax = 0.0145;
s1.d1.hitax = 0.0145;
s2.d1.hitax = 0.0145;
s3.d1.hitax = 0.0145;
s4.d1.hitax = 0.0145;
b1.d1.hitax = 0.0145;
b2.d1.hitax = 0.0145;
b3.d1.hitax = 0.0145;

//Prompt user for payroll data

cout << "Please enter Manager ID: ";
cin >> m1.ID;

cout << "\nPlease enter Manager rate of pay: ";
cin >> m1.rate;

cout << "\nPlease enter Manager hours worked this week: ";
cin >> m1.hours;

cout << "\nPlease enter Manager ID: ";
cin >> m2.ID;

cout << "\nPlease enter Manager rate of pay: ";
cin >> m2.rate;

cout << "\nPlease enter Manager hours worked this week: ";
cin >> m2.hours;

cout << "\nPlease enter Sales Associate ID: ";
cin >> s1.ID;

cout << "\nPlease enter Sales Associate rate of pay: ";
cin >> s1.rate;

cout << "\nPlease enter Sales Assoicate hours worked this week: ";
cin >> s1.hours;

cout << "\nPlease enter Sales Associate ID: ";
cin >> s2.ID;

cout << "\nPlease enter Sales Associate rate of pay: ";
cin >> s2.rate;

cout << "\nPlease enter Sales Assoicate hours worked this week: ";
cin >> s2.hours;

cout << "\nPlease enter Sales Associate ID: ";
cin >> s3.ID;

cout << "\nPlease enter Sales Associate rate of pay: ";
cin >> s3.rate;

cout << "\nPlease enter Sales Assoicate hours worked this week: ";
cin >> s3.hours;

cout << "\nPlease enter Sales Associate ID: ";
cin >> s4.ID;

cout << "\nPlease enter Sales Associate rate of pay: ";
cin >> s4.rate;

cout << "\nPlease enter Sales Assoicate hours worked this week: ";
cin >> s4.hours;

cout << "\nPlease enter Book Keeper ID: ";
cin >> b1.ID;

cout << "\nPlease enter Book Keeper rate of pay: ";
cin >> b1.rate;

cout << "\nPlease enter Book Keeper hours worked: ";
cin >> b1.hours;

cout << "\nPlease enter Book Keeper ID: ";
cin >> b2.ID;

cout << "\nPlease enter Book Keeper rate of pay: ";
cin >> b2.rate;

cout << "\nPlease enter Book Keeper hours worked: ";
cin >> b2.hours;

cout << "\nPlease enter Book Keeper ID: ";
cin >> b3.ID;

cout << "\nPlease enter Book Keeper rate of pay: ";
cin >> b3.rate;

cout << "\nPlease enter Book Keeper hours worked: ";
cin >> b3.hours;


//Calculate data

if(m1.hours > 40)

{
m1.d1.overtime = m1.hours-40;
m1.hours = 40;
}

m1.d1.gpay = (m1.hours * m1.rate) + (m1.d1.overtime * m1.rate * 1.5);

m1.d1.oasditax = m1.d1.gpay * m1.d1.oasditax;

m1.d1.hitax = m1.d1.gpay * m1.d1.hitax;

if (m1.d1.gpay <= 101)
{
m1.d1.fedtax = 0.058;
}
else if ( m1.d1.gpay >= 101.01 && m1.d1.gpay <= 201)
{
m1.d1.fedtax = 0.153;
}
else if (m1.d1.gpay >= 201.01 && m1.d1.gpay <= 351)
{
m1.d1.fedtax = 0.184;
}
else if (m1.d1.gpay >= 351.01 && m1.d1.gpay <= 502)
{
m1.d1.fedtax = 0.239;
}
else
{
m1.d1.fedtax = 0.288;
}

m1.d1.fedtax = m1.d1.fedtax * m1.d1.gpay;

m1.d1.npay = m1.d1.gpay - m1.d1.oasditax - m1.d1.hitax - m1.d1.fedtax;

//display user data by department

cout << "\n" << m1.ID << "\n" << m1.rate << "\n" << m1.hours << "\n" << m1.d1.gpay << "\n" << m1.d1.fedtax << "\n" << m1.d1.oasditax << "\n" <<
m1.d1.hitax << "\n" << m1.d1.npay << "\n";


return 0;
}
calculations for the gross pay, federal tax and other calculations is where I got lost

Do you know how to calculate these for 1 employee?
Last edited on
To calculate for one it would look something like this-

if (hours > 40)

{
overtime = hours-40;
hours = 40;
}

gpay = (hours * rate) + (overtime * rate * 1.5);

oasditax = gpay * oasditax;

hitax = gpay * hitax;

if (gpay <= 101)
{
fedtax = 0.058;
}
else if ( gpay >= 101.01 && gpay <= 201)
{
fedtax = 0.153;
}
else if (gpay >= 201.01 && gpay <= 351)
{
fedtax = 0.184;
}
else if (gpay >= 351.01 && gpay <= 502)
{
fedtax = 0.239;
}
else
{
fedtax = 0.288;
}

fedtax = fedtax * gpay;

npay = gpay - oasditax - hitax - fedtax;
add the 9 employees to an array.
create a function to do the calculations for one.
void calculate(struct EmpInfo employee)

The go through the array calling the function for every employee.

Use code tags..
Topic archived. No new replies allowed.