'How can I fix my program's calculation and get it to run properly?
My code isn’t calculating. I tried for hours but it still showing 0.00 for gross taxes and union fee. and of course its not calculating the gross total and average. I’m at a lost where I went wrong. I reviewed so many example's of calculating with structs and I still cannot find as to why its showing 0.
Here is my code :
#include <iomanip>
#include <string>
using namespace std;
struct Payroll
{
string f_name, l_name;
char m_initial;
int hrs_worked;
double rate, gross, state_tax,fed_tax, union_fees,net;
};
int main()
{
int size=0, num=20;
Payroll workers[num];
double overtime, gross, fed, state, unionFees, net;
double total_gross;
cout<<"Enter the number of employees: ";// take input of number of employees
cin >> num;
//get employee's info
cout<<"Enter details of employee " <<size+1<<": "<<endl;
for (size =0; size < num; size ++)
{
//asking for user's input
cout<<"Enter first name : ";
cin >> workers[size].f_name;
cout<<"Enter middle name initial: ";
cin >> workers[size].m_initial;
cout<<"Enter last name : ";
cin >> workers[size].l_name;
cout<<"Enter hours worked : ";
cin >> workers[size].hrs_worked;
//input validation
while(workers[size].hrs_worked < 0 || workers[size].hrs_worked > 60)
{
cout<<"Error! Enter hours between 0 to 60: ";
cin >> workers[size].hrs_worked;
}
cout<<"Enter Rate per hour : ";
cin >> workers[size].rate;
// input validation
while(workers[size].rate <= 0 || workers[size].rate > 50)
{
cout<<"Error! Enter rate greater than 0 to 50: ";
cin >> workers[size].rate;
}
cout<< endl;
}
// Calculating each employee's pay
if(workers[size].hrs_worked > 40)
{
overtime = (workers[size].hrs_worked - 40) * workers[size].rate * 1.5;
workers[size].gross = (workers[size].rate * 40) + overtime;
}
gross = (workers[size].rate * workers[size].hrs_worked); // calculate gross
state = workers[size].gross * 6/100;// calculate state tax
fed = workers[size].gross * 12/100;// calculate federal tax
unionFees = workers[size].gross * 2/100;// calculate union fees
net = workers[size].gross - (workers[size].state_tax + workers[size].fed_tax + workers[size].union_fees);// calculate net
total_gross= workers[size].gross+total_gross;
// display header
cout<<"Data Housing Corp. Weekly Payroll"<<endl;
cout<<setw(8)<<"FN"<<setw(4)<<"MI"<<setw(8)<<"LN"<<setw(8)<<"Hours"<<setw(6)
<<"Rate"<<setw(8)<<"Gross"<<setw(6)<<"ST"<<setw(8)<<"FT"<<setw(8)
<<"UF"<<setw(8)<<"Net"<<endl;
cout<<setw(8)<<"==="<<setw(4)<<"==="<<setw(8)<<"==="<<setw(8)<<"===="<<setw(6)
<<"===="<<setw(8)<<"===="<<setw(6)<<"==="<<setw(8)<<"==="<<setw(8)
<<"==="<<setw(8)<<"===="<<endl;
// displaying data
for (size =0; size < num; size ++)
{
cout<<setw(8)<<workers[size].f_name<<setw(4);
cout<<workers[size].m_initial<<setw(8);
cout<<workers[size].l_name<<setw(6);
cout<<workers[size].hrs_worked<<setw(8);
cout<<fixed <<setprecision(2)<<workers[size].rate<<setw(8);
cout<<fixed <<setprecision(2)<<workers[size].gross<<setw(8);
cout<<fixed <<setprecision(2)<<state<<setw(8);
cout<<fixed <<setprecision(2)<<fed<<setw(8);
cout<<fixed <<setprecision(2)<<unionFees<<setw(8);
cout<<fixed <<setprecision(2)<<net<<endl;
}
cout<<"========== =========== ========= ============= =========== ========";
cout<<" ============ ========= =========== ========== ==========\n";
cout<<"Total gross income of all employee= $"<<total_gross<<endl;
cout<<"Average gross income of all employee= $"<<total_gross/num<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
return 0;
}
Enter the number of employees: 2
Enter details of employee 1:
Enter first name : laaa
Enter middle name initial: d
Enter last name : daaaa
Enter hours worked : 50
Enter Rate per hour : 10
Enter first name : rome
Enter middle name initial: s
Enter last name : italy
Enter hours worked : 40
Enter Rate per hour : 10
Data Housing Corp. Weekly Payroll
FN MI LN Hours Rate Gross ST FT UF Net
=== === === ==== ==== ==== === === === ====
laaa d daaaa 50 10.00 0.00 0.00 0.00 0.00 0.00
rome s italy 40 10.00 0.00 0.00 0.00 0.00 0.00
========== =========== ========= ============= =========== ======== ============ ========= =========== ========== ==========
Total gross income of all employee= $nan
Average gross income of all employee= $nan
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Solution 1:[1]
You should note that the conditional statement you have (if(workers[size].hrs_worked > 40)) is out of your for loop, yet you use that loop counter variable 'size' within that conditional statement.
A few side notes:
You need to initialize 'workers[size].gross' even if the 'workers[size].hrs_worked' attribute is <= 40.
It seems as though an array isn't an ideal option over here (unless you use dynamic memory allocation) since you are initializing the size of the array at runtime. Not sure how your compiler is allowing you to do this?
'total_gross' is uninitialized but you use it in some arithmetic down the line; clarify its use and the other local variable's use as well.
You should be able to take it from here; happy coding!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ipudu |
