I need to enter info and calculate some stuff for several employees, and output every employees information into single console, and I need to use arrays.
Problem is I don't really know how to store information from the loop into an array. Screenshot of exercise is here
I ask the user how many workers there are, and the value goes into "workers" variable, then I create int employees[workers] array, so the number of iterations in the loop is determined by user's input.
The problem with my loop is that it does not reiterate the questions no matter how many employees there are.
I used do while loop and "Count" variable to control the number of reiterations, but after entering the info once, it just shows the result, instead of asking the questions again.
I also tried while loop and "count" variable, but this time it asks only how many employees there are and it just shows empty output.
int main()
{
//************************DECLARATIONS**********************
typedef char INFO;
INFO f_name[30];
INFO m_name[10];
INFO l_name[30];
int count; // tracks the number of iterations in do loop
int workers;
double rate;
double hrs_worked;
double gross_inc;
double overtime;
double tax_total;
float net;
float STATE_TAX;
float FED_TAX;
float UNI_FEE;
const double OVERTIME_R = 1.5;
//*****************INPUT FROM USER***************************
cout << "Enter amount of workers" << endl;
cin >> workers;
int employees[workers];
while(count < workers)
{
cout << "Enter worker's First name: " << endl;
cin.getline(f_name, (sizeof(f_name)-1));
cout << "Enter worker's middle name initial: " << endl;
cin.getline(m_name, (sizeof(m_name)-1));
cout << "Enter worker's last name: " << endl;
cin.getline(l_name, (sizeof(l_name)-1));
cout << "Enter number of hours worked: " << endl;
cin >> hrs_worked;
// If statement activates if user enters incorrect values
// and asks to reenter the correct value.
if(hrs_worked < 0 || hrs_worked > 60)
{
while(hrs_worked < 0 || hrs_worked > 60)
{
cout << "Must be between 0 and 60: " << endl;
cin >> hrs_worked;
}
}
cout << "Enter Rate Per Hour: " << endl;
cin >> rate;
// If statement activates if user enters incorrect values
// and asks to reenter the correct value.
if(rate < 0 || rate > 50)
{
while(rate < 0 || rate > 50)
{
cout << "Must be between 0 and 50: " << endl;
cin >> rate;
}
}
count++;
}
system("clear");
//**********************************CALCULATIONS*****************
// Calculates overtime if employee worked more than 40 hrs
if(hrs_worked > 40)
{
overtime = (hrs_worked - 40.0) * rate * OVERTIME_R;
}
gross_inc = (rate * hrs_worked) + overtime;
STATE_TAX = gross_inc * 0.06;
FED_TAX = gross_inc * 0.12;
UNI_FEE = gross_inc * 0.02;
tax_total = STATE_TAX + FED_TAX + UNI_FEE;
net = gross_inc - (tax_total)
return 0;
}
At the moment priority is to set up a correct loop and store information from the loop into an array. Output is not the main focus at this point.