Parallel arrays

I need help desperately with an array! please someone, help?
Objective:
The purpose of this project is to expose you to:
One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions.

Problem Specification:
The PC Pine Furniture Company has recently hired you to help them generate a payroll report for all their employees. You are given a data file containing the employee’s data. The data for each employee consists of the employee id, name, hours worked and rate of pay. The number of employees in the file is unknown. However; the last employee id number is 0.You are to read this data into parallel arrays, calculate the gross pay, federal withholdings, state withholdings, hospitalization and net pay for each employee and store each in an array. You should define the followings as constants:

• Federal withholding tax rate: 18%
• State withholding tax rate: 4.5%
• Social Security withholding: 2%

Your output should be:
• A report for all employees written to a file and consisting of the employee name, id number, gross pay and net pay.

• Another report written to a different file containing the total gross pay paid to all employees, total federal withholdings, total state withholdings, total Social Security withholding and total for all withholdings.

Requirements:
• Comment thoroughly.
• Every function must have specifications in the form of comments.
• Create global constants.
• Make sure every function accomplishes a task and is self-contained
• Use typedef to define new types as necessary.
• Each file has a heading and column labels to identify information.
• Must test if data file exists before reading data.
• You may need to write additional functions such as total which returns the total of an array.
• main is used as a driver function, no input/output or calculations is done in main, only what is specified.

a counter is used to count the number of elements read.
generate an error if file does not exist, or if not open.
a loop reads the data until the trailing value 0 for ID is encountered.
each function have clear, full and thorough specifications.
the function setdata () reads the data from the file into arrays.
the function setdata() returns the number of employees read from the file as its value.
the function findgross () fills the gross array with calculated data.
the function findwithholding () accepts the needed data for an employee and returns the withholdings.
the function findwithholding () is reused to calculate different withholding.
the function findnet() fills the net array with calculated data.
the function total () will return the total of its array argument. (reused).
headings (), prints the headings and column titles, data/calculated results are printed
Last edited on
this is what I've come up with!!! I can't tell you how much sleep I've lost. Please help me someone!?!?


#include <iostream>
#include <fstream>
#include <iomanip>



using namespace std;

const float FEDERALWITH_TAXRATE =.18;
const float STATEWITH_TAXRATE = .045;
const float SOCIALSEC_WITHHOLD = .02;

const int Size = 30;

typedef long int employee[Size];
typedef int intarray[Size];
typedef float fltarray[Size];
typedef string strarray[Size];


int setdata(employee,strarray,intarray,fltarray,ifstream &,fltarray,fltarray,fltarray,fltarray,fltarray,int &);

float findgross(intarray,fltarray,fltarray,int,float &);

void printdata(const employee,const strarray,const intarray,const fltarray, const fltarray,
const fltarray, const fltarray);

void findwithholding(fltarray,int,float,fltarray);

int main()
{
employee id;
int num = 0, count;
float totalgross;
strarray name;
intarray hours;
fltarray rate,gross,net,taxes,social,state;
ifstream fin("empdata.txt");

count = setdata(id,name,hours,rate,fin,gross,net,taxes,social,state,num);
findgross(hours,rate,gross,count,totalgross);
findwithholding(gross, count,FEDERALWITH_TAXRATE, taxes);
findwithholding(gross, count, STATEWITH_TAXRATE, state);
findwithholding(gross, count, SOCIALSEC_WITHHOLD, social);
printdata(id,name,hours,rate,gross,net,taxes);


return 0;
}

int setdata(employee id,strarray name,intarray hrs,fltarray rate,ifstream & fin,
fltarray gross,fltarray net,fltarray taxes,fltarray social,fltarray state,int &i)
{


//int i = 0;


fin>>id[i];
while(id[i]!=0){
fin.get();
getline(fin, name[i]);
fin>>hrs[i];
fin>>rate [i];
i++;
fin>>id[i];


/*for(int i = 0; i < Size; i++)
{ fin >> id[i]>>hrs[i]>>rate[i];
calc(hrs[i],rate[i],gross[i],taxes[i],state[i],social[i],net[i]);
}*/

}
return i;
}

float findgross(intarray hrs, fltarray rate, fltarray gross, int count,float &totalgross)
{
for (int i = 0; i < count; i++)
{
gross[i] = rate[i]*hrs[i];
totalgross += gross[i];
}
return totalgross;
}


void findwithholding(fltarray gross, int count, float withhold, fltarray income)
{
for (int i = 0; i < count; i++)
{
income[i] = gross[i]*withhold;
}
}

/*void findgross(int hours, float payrate, float &grosspay,float &fedtax,float &statetax,float &soc_withhold,float &netpay)
{ grosspay = hours * payrate;
fedtax = grosspay * FEDERALWITH_TAXRATE;
statetax = grosspay * STATEWITH_TAXRATE;
soc_withhold = grosspay * SOCIALSEC_WITHHOLD;
netpay = (grosspay - (fedtax+statetax+soc_withhold));


}*/

void printdata(const employee id,const strarray name,const intarray hrs,const fltarray rate,
const fltarray gross,const fltarray net,const fltarray taxes)
{ //cout <<setiosflags(ios::fixed|ios::showpoint)<<setprecision(2);
for(int j = 0; j < Size; j++)
{
cout << setprecision(2);
cout << left << setw(15) << "Employee ID";
cout << right << setw(20) << "Employee Name";
cout << right << setw(20) << "Employee Hours";
cout << right << setw(20) << "Employee Rate";
cout << right << setw(20) << "Employee Gross Pay";
cout << right << setw(20) << "Employee Net Pay";
cout << endl;

for(int j=0;j<Size;j++){
cout << left << setw(15) <<id[j];
cout << right << setw(20) <<name[j];
cout << right << setw(20) <<hrs[j];
cout << right << setw(20) <<rate[j];
cout << right << setw(20) <<gross[j];
cout << right << setw(20) <<net[j] <<endl;
}


//cout <<setw(10)<< id[i]<<setw(8)<<name[i]<<setw(6)<<hrs[i]<<setw(4)<<rate[i];
//cout << setw(10)<<gross[i] <<setw(8)<< net[i]<<endl;
//cout << id[i] << " " << name[i] <<" " <<hrs[i] <<" " <<rate[i] << endl;
}
}
Please help me someone!?!?


What's wrong with your program?
Please use code tags.
Please provide example input.
Last edited on
I did, it's all there... I posted the entire thing
Here's the data file



211692
Ahmed, Marco
47 12.50
240885
ATamimi, Trevone
30 15.00
281393
Choudhury, Jacob
10 10.00
272760
De la Cruz, Edward
25 22.25
199593
Edwards, Faraj
60 11.75
256109
Edwards, Bill
45 30.50
246779
Gallimore, Christian
08 15.00
270081
Lopez, Luis
18 16.00
114757
Mora, Sam
37 13.20
270079
Moses, Samuel
48 9.50
193280
Perez, Albert
52 17.75
252830
Rivas, Jonathan
32 23.25
252830
Robinson, Albert
39 11.00
276895
Miranda, Michael
28 18.00
280515
Robinson, Iris
45 22.25
0000
Thanks for the sample input!

What's wrong with your program?
Please use code tags.

Note:
It is usually useful to force yourself to accurately explain the symptoms of the problem.
https://en.wikipedia.org/wiki/Rubber_duck_debugging

Further, without such a description we're left with a large blob of code and no idea what to look for. All you need to do is explain what's wrong with it: a statement or two in the form of "what you expected", vs. "what you got" (i.e., object-deviation format). You'll get better help this way.
Last edited on
It doesn't output the correct information... it runs, just not efficiently. I can't seem output the information into each array
I'm not sure what you're asking for but here goes: The information I sent above is thoe code I already wrote in C++, where the requirements were to use parallel arrays. I was supposed to calculate the gross pay, netpay and the taxes. The program came close but the output was part garbage and part good. Better? I don't know what else to say, I'm dying here....
closed account (48T7M4Gy)
Well, here's a substantial start. You're making too much of a meal out of this and obviously haven't got a plan to work to.

The first step is stated in the problem specification and that is to read in the data and allocate the various bits to parallel arrays.

Then and only then should you start processing it. (I've deliberately started processing in the loop but that is just an expedient for me.) Also I wouldn't complicate the work by starting off worrying about first and last names or formatting the data. They are the last things to worry about.

You'll have to change the file name to suit what you have.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int no_employees = 30;

int main () {
    
    int ID[no_employees]{};
    string name[no_employees];
    int hours[no_employees];
    double rate[no_employees];
    
    int count = 0;
    
    ifstream myfile ("employees.txt");
    if (myfile.is_open())
    {
        while( myfile >> ID[count] && ID[count] != 0)
        {
            myfile.ignore(1000, '\n');
            getline( myfile, name[count]);
            
            myfile >> hours[count];
            myfile >> rate[count];
            
            cout
            << ID[count] << ' '
            << name[count] << ' '
            << hours[count] << ' '
            << rate[count]
            << " Gross pay $" << hours[count] * rate[count]
            << '\n';
            count++;
        }
        cout << "There are " << count << " employees.\n";
        myfile.close();
    }
    
    else cout << "Unable to open file";
    
    return 0;
}


211692 Ahmed, Marco 47 12.5 Gross pay $587.5
240885 ATamimi, Trevone 30 15 Gross pay $450
281393 Choudhury, Jacob 10 10 Gross pay $100
272760 De la Cruz, Edward 25 22.25 Gross pay $556.25
199593 Edwards, Faraj 60 11.75 Gross pay $705
256109 Edwards, Bill 45 30.5 Gross pay $1372.5
246779 Gallimore, Christian 8 15 Gross pay $120
270081 Lopez, Luis 18 16 Gross pay $288
114757 Mora, Sam 37 13.2 Gross pay $488.4
270079 Moses, Samuel 48 9.5 Gross pay $456
193280 Perez, Albert 52 17.75 Gross pay $923
252830 Rivas, Jonathan 32 23.25 Gross pay $744
252830 Robinson, Albert 39 11 Gross pay $429
276895 Miranda, Michael 28 18 Gross pay $504
280515 Robinson, Iris 45 22.25 Gross pay $1001.25
There are 15 employees.
Program ended with exit code: 0
Last edited on
Thanks for adding specifics!

Inside printData you are printing the entire table Size times.
You only need to print it once.

You also need to keep track of the number of records read, so that you don't print the empty entries in the table (which are full of garbage). You return a count parameter that does this, but you never pass it into the printData function.

You can change printData() to look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void printdata(const employee id,const strarray name,const intarray hrs,const fltarray rate,
               const fltarray gross,const fltarray net,const fltarray, int const count) {
  using std::left; using std::right; using std::setw;
  std::cout << std::setprecision(2)
            << left  << setw(15) << "Employee ID"
            << right << setw(20) << "Employee Name"
            << right << setw(20) << "Employee Hours"
            << right << setw(20) << "Employee Rate"
            << right << setw(20) << "Employee Gross Pay"
            << right << setw(20) << "Employee Net Pay" << std::endl;

  for(int i=0;i<count;i++) {
    std::cout << left  << setw(15) << id   [i]
              << right << setw(20) << name [i]
              << right << setw(20) << hrs  [i]
              << right << setw(20) << rate [i]
              << right << setw(20) << gross[i]
              << right << setw(20) << net  [i] << std::endl;
  }
}

Change the function declaration and the call to match.
Last edited on
OK.. cool. On it
closed account (48T7M4Gy)
This shows you how to modularize the program using simple functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


const int no_employees = 30;


int setData(string, int[], string[], int[], double[], int);
void printData(int[], string[], int[], double[], int);


int main () {
    
    int ID[no_employees]{};
    string name[no_employees];
    int hours[no_employees];
    double rate[no_employees];
    
    
    int actual_count = setData("employees.txt", ID, name, hours, rate, no_employees);
    cout << "Count: " << actual_count << '\n';
    
    
    printData(ID, name, hours, rate, actual_count);
    
    
    return 0;
}

int setData(string aFile, int aID[], string aName[], int aHours[], double aRate[], int aNo_employees){
    
    int count = 0;
    
    
    ifstream myfile (aFile);
    if (myfile.is_open())
    {
        while( myfile >> aID[count] && aID[count] != 0)
        {
            myfile.ignore(1000, '\n');
            getline( myfile, aName[count]);
            
            myfile >> aHours[count];
            myfile >> aRate[count];
            count++;
        }
        
        
        cout << "There are " << count << " employees.\n";
        myfile.close();
    }
    else cout << "Unable to open file";
    return count;
}


void printData(int aID[], string aName[], int aHours[], double aRate[], int aNo_employees){
    
    for(int i = 0; i < aNo_employees; ++i){
        cout
        << i << ' '
        << aID[i] << ' '
        << aName[i] << ' '
        << aHours[i] << ' '
        << aRate[i]
        << " Gross pay $" << aHours[i] * aRate[i]
        << '\n';
    }
}


There are 15 employees.
Count: 15
0 211692 Ahmed, Marco 47 12.5 Gross pay $587.5
1 240885 ATamimi, Trevone 30 15 Gross pay $450
2 281393 Choudhury, Jacob 10 10 Gross pay $100
3 272760 De la Cruz, Edward 25 22.25 Gross pay $556.25
4 199593 Edwards, Faraj 60 11.75 Gross pay $705
5 256109 Edwards, Bill 45 30.5 Gross pay $1372.5
6 246779 Gallimore, Christian 8 15 Gross pay $120
7 270081 Lopez, Luis 18 16 Gross pay $288
8 114757 Mora, Sam 37 13.2 Gross pay $488.4
9 270079 Moses, Samuel 48 9.5 Gross pay $456
10 193280 Perez, Albert 52 17.75 Gross pay $923
11 252830 Rivas, Jonathan 32 23.25 Gross pay $744
12 252830 Robinson, Albert 39 11 Gross pay $429
13 276895 Miranda, Michael 28 18 Gross pay $504
14 280515 Robinson, Iris 45 22.25 Gross pay $1001.25
Program ended with exit code: 0


Now it's your turn.
Last edited on
So far, so good... except I'm unable to figure why the calculations come out so weirdly
So grateful for all your help from both of you. However, the premise is that I don't know how many employees there are... Please pardon my ignorance, I've only started doing this January 18th and the course is moving extremely fast without much explanation
Ooooooh I see now!
Topic archived. No new replies allowed.