Array help... please

Hi all - I can't get ahold of my tutor and it's due Monday. I can't get my arrays to work. I know the information is going into the array, but when I run displayEmployeeResults I get crazy results for all entries except the last employee for whom I entered data. It's in 2 pieces bc of the 9000 character limit. This is the top piece.

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

void getInput(int empID[],int hoursWorked[],int payRate[],int unionCode[],char payrollType[],int arrayIndex);

void getInput(int empID[],int hoursWorked[],int payRate[],int unionCode[],char payrollType[],int arrayIndex){
    int id;
    int pay;
    int hours;
    int code;

    char payroll;

    arrayIndex++;

    cout << "Enter employee ID " <<endl;
    cin >> id;


    while( id >800 || id <100 )
      {
        cout << "Invalid entry. Please enter employee ID " << endl;
        cin >> id;
      }
    empID[arrayIndex] = id;

      cout << "Enter payroll type " << endl;
      cin >> payroll;
    //payrollType input section

    while ( payroll != 'h' && payroll != 'H'){
                  cout << "Invalid entry. Please enter payroll type " << endl;
                  cin >> payroll;//assigns input to a declared variable
                  }
    payrollType[arrayIndex] = payroll;

    cout << "Enter hours worked " << endl;
    cin >> hours;
    
    //hoursWorked input section
    
    while( hours >60 || hours <0 )
      {
        cout << "Invalid entry. Please enter hours worked " << endl;
        cin >> hours;//assigns input to a declared variable
      }
    hoursWorked[arrayIndex]= hours;
    
    //payRate input section
    
    cout << "Enter pay rate " << endl;
    cin >> pay;
    
    while( pay >45.0 || pay <8.5 )
      {
        cout << "Invalid entry. Please enter pay rate " << endl;
        cin >> pay;
      }
    payRate[arrayIndex]= pay;
    //unionCode input section
      
    cout << "Enter union code " << endl;
    cin >> code;
    
    while( code != 1 && code != 2 && code != 3 )
           {
           cout << "Invalid entry. Please enter union code " << endl;
           cin >> code;
           }
           unionCode[arrayIndex]= code;

}

double doCalculations(int empID[],int hoursWorked[],int payRate[],int unionCode[],char payrollType[],int arrayIndex,double regularPay[],double overtimePay[],double grossPay[],double netPay[],double stateTax[],double federalTax[],double totalTax[], double unionDues[]);

double doCalculations(int empID[],int hoursWorked[],int payRate[],int unionCode[],char payrollType[],int arrayIndex,double regularPay[],double overtimePay[],double grossPay[],double netPay[],double stateTax[],double federalTax[],double totalTax[], double unionDues[]){
                
                arrayIndex++;
                
                int pay;
                double regular;
                double overtime;
                double gross;
                double net;
                double state;
                double federal;
                double total;
                int dues;  
 
                if ( hoursWorked[arrayIndex] > 40 ){
                   regular = payRate[arrayIndex] * hoursWorked[arrayIndex];
                   overtime = 1.5 * payRate[arrayIndex] * (hoursWorked[arrayIndex] - 40);
                   gross = regular + overtime;
                   }
                else if ( hoursWorked[arrayIndex] <= 40 ){
                     gross = payRate[arrayIndex] * hoursWorked[arrayIndex];
                     regular = payRate[arrayIndex] * hoursWorked[arrayIndex];
                     overtime = 0;
                     }
                
                regularPay[arrayIndex]=regular;
                grossPay[arrayIndex]=gross;
                overtimePay[arrayIndex]=overtime;
                payRate[arrayIndex]=pay;
                
                cout << "\nPayroll type = " << payrollType[arrayIndex] << endl;
                cout << "Regular pay = " << regularPay[arrayIndex] << endl;
                cout << "Overtime pay = " << overtimePay[arrayIndex] << endl;
                cout << "Gross pay = " << grossPay[arrayIndex] << endl;
                if (grossPay[arrayIndex] < 500)
                    state = 0.0;//if grossPay < 500, stateTax is 0
                else if (grossPay[arrayIndex] >= 1000)
                        state = grossPay[arrayIndex] * 0.05;
                        else
                        state = grossPay[arrayIndex] * 0.03;
                        
                        cout << "State tax = " << state << endl;
                        stateTax[arrayIndex]=state;
                        
                 if (grossPay[arrayIndex] < 500)
                    federal = 0.0;//if grossPay < 500, federalTax is 0
                 else if (grossPay[arrayIndex] >= 1000)
                        federal = grossPay[arrayIndex] * 0.07;
                        else
                        federal = grossPay[arrayIndex] * 0.05;
                        
                        cout << "Federal tax = " << federal << endl;
                        
                        federalTax[arrayIndex]=federal;
                        
                total = stateTax[arrayIndex] + federalTax[arrayIndex];//totalTax formula
                cout << "Total tax = " << total << endl;
                     totalTax[arrayIndex]=total;   
                

                switch(unionCode[arrayIndex]){
                  case 1: unionDues[arrayIndex] = 15;
                  break;
                  case 2: unionDues[arrayIndex] = 25;
                  break;
                  case 3: unionDues[arrayIndex] = 35;
                  break;
                  }
                
                cout << "Union dues = " << unionDues[arrayIndex] << endl;

                netPay[arrayIndex] = grossPay[arrayIndex] - (totalTax[arrayIndex] + unionDues[arrayIndex]);
                
                cout << "Net pay = " << netPay[arrayIndex] << endl;
}
The problematic array is the big list of for statements right here:

Part 2:
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

double displayEmployeeResults(int empID[],int hoursWorked[],int payRate[],int unionCode[],char payrollType[],int arrayIndex,double regularPay[],double overtimePay[],double grossPay[],double netPay[],double stateTax[],double federalTax[],double totalTax[], double unionDues[]);
double displayEmployeeResults(int empID[],int hoursWorked[],int payRate[],int unionCode[],char payrollType[],int arrayIndex,double regularPay[],double overtimePay[],double grossPay[],double netPay[],double stateTax[],double federalTax[],double totalTax[], double unionDues[]){
          
          int i;
          
          cout <<"             Entry1       Entry2       Entry3       Entry4"<<endl;
          cout <<"Employee ID  ";
          for (i=0;i<4;i++){
              cout<<"             "<<empID[i]<<"            "<<empID[i+1];
              }
              cout<<endl;
          cout <<"Payroll Type ";
          for (i=0;i<4;i++){
              cout<<"             "<<payrollType[i];
              }
              cout<<endl;
          cout <<"Total Hours  ";
          for (i=0;i<4;i++){
              cout<<"             "<<hoursWorked[i];
              }
              cout<<endl;
          cout <<"Pay Rate     ";
          for (i=0;i<4;i++){
              cout<<"             "<<payRate[i];
              }
              cout<<endl;
          cout <<"Union Code   ";
          for (i=0;i<4;i++){
              cout<<"             "<<unionCode[i];
              }
              cout<<endl;
          cout <<"Regular Pay  ";
          for (i=0;i<4;i++){
              cout<<"             "<<regularPay[i];
              }
              cout<<endl;
          cout <<"Overtime Pay ";
          for (i=0;i<4;i++){
              cout<<"             "<<overtimePay[i];
              }
              cout<<endl;
          cout <<"Gross Pay    ";
          for (i=0;i<4;i++){
              cout<<"             "<<grossPay[i];
              }
              cout<<endl;
          cout <<"State Tax    ";
          for (i=0;i<4;i++){
              cout<<"             "<<stateTax[i];
              }
              cout<<endl;
          cout <<"Federal Tax  ";
          for (i=0;i<4;i++){
              cout<<"             "<<federalTax[i];
              }
              cout<<endl;
          cout <<"Total Tax    ";
          for (i=0;i<4;i++){
              cout<<"             "<<totalTax[i];
              }
              cout<<endl;
          cout <<"Union Dues   ";
          for (i=0;i<4;i++){
              cout<<"             "<<unionDues[i];
              }
              cout<<endl;
          cout <<"Net Pay      ";
          for (i=0;i<4;i++){
              cout<<"             "<<netPay[i];
              }
              cout<<endl;
}

double displayPayrollSummaryResults();

int main(int argc, char *argv[])
{
    int empID[4];
    int hoursWorked[4];
    int payRate[4];
    int unionCode[4];
    char payrollType[4];
    double unionDues[4];
    double regularPay[4];
    double overtimePay[4];
    double grossPay[4];
    double stateTax[4];
    double federalTax[4];
    double totalTax[4];
    double netPay[4];
    int arrayIndex =0;    
    char carryOn;
    int i;
    
    cout << "Payroll Calculator" << endl;

    do {
    getInput(empID,hoursWorked,payRate,unionCode,payrollType,arrayIndex);

   doCalculations(empID,hoursWorked,payRate,unionCode,payrollType,arrayIndex,regularPay,grossPay,overtimePay,stateTax,federalTax,totalTax,unionDues,netPay);
    
          cout << "Would you like to calculate another employee's payroll? [Y/N]" << endl;
          cin >> carryOn;
          }
    while (carryOn == 'Y' || carryOn == 'y');
    
    while( carryOn != 'N' && carryOn != 'n' && carryOn != 'Y' && carryOn != 'y' ){
           cout<< "Invalid Entry. Would you like to calculate another employee's payroll? [Y/N]"<<endl;
           cin>> carryOn;
    if (carryOn == 'N' || carryOn == 'n'){
              displayEmployeeResults(empID,hoursWorked,payRate,unionCode,payrollType,arrayIndex,regularPay,grossPay,overtimePay,stateTax,federalTax,totalTax,unionDues,netPay);      
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}


You're a legend if you can help me with this...
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

void getInput(int empID[],int hoursWorked[],int payRate[],int unionCode[],char payrollType[],int arrayIndex);

void getInput(int empID[],int hoursWorked[],int payRate[],int unionCode[],char payrollType[],int arrayIndex){
    int id;
    int pay;
    int hours;
    int code;

    char payroll;

    arrayIndex++;

    cout << "Enter employee ID " <<endl;
    cin >> id;


    while( id >800 || id <100 )
      {
        cout << "Invalid entry. Please enter employee ID " << endl;
        cin >> id;
      }
    empID[arrayIndex] = id;

      cout << "Enter payroll type " << endl;
      cin >> payroll;
    //payrollType input section

    while ( payroll != 'h' && payroll != 'H'){
                  cout << "Invalid entry. Please enter payroll type " << endl;
                  cin >> payroll;//assigns input to a declared variable
                  }
    payrollType[arrayIndex] = payroll;

    cout << "Enter hours worked " << endl;
    cin >> hours;
    
    //hoursWorked input section
    
    while( hours >60 || hours <0 )
      {
        cout << "Invalid entry. Please enter hours worked " << endl;
        cin >> hours;//assigns input to a declared variable
      }
    hoursWorked[arrayIndex]= hours;
    
    //payRate input section
    
    cout << "Enter pay rate " << endl;
    cin >> pay;
    
    while( pay >45.0 || pay <8.5 )
      {
        cout << "Invalid entry. Please enter pay rate " << endl;
        cin >> pay;
      }
    payRate[arrayIndex]= pay;
    //unionCode input section
      
    cout << "Enter union code " << endl;
    cin >> code;
    
    while( code != 1 && code != 2 && code != 3 )
           {
           cout << "Invalid entry. Please enter union code " << endl;
           cin >> code;
           }
           unionCode[arrayIndex]= code;

}

double doCalculations(int empID[],int hoursWorked[],int payRate[],int unionCode[],char payrollType[],int arrayIndex,double regularPay[],double overtimePay[],double grossPay[],double netPay[],double stateTax[],double federalTax[],double totalTax[], double unionDues[]);

double doCalculations(int empID[],int hoursWorked[],int payRate[],int unionCode[],char payrollType[],int arrayIndex,double regularPay[],double overtimePay[],double grossPay[],double netPay[],double stateTax[],double federalTax[],double totalTax[], double unionDues[]){
                
                arrayIndex++;
                
                
                double regular;
                double overtime;
                double gross;
                
                double state;
                double federal;
                double total;
                
 
                if ( hoursWorked[arrayIndex] > 40 ){
                   regular = payRate[arrayIndex] * hoursWorked[arrayIndex];
                   overtime = 1.5 * payRate[arrayIndex] * (hoursWorked[arrayIndex] - 40);
                   gross = regular + overtime;
                   }
                else if ( hoursWorked[arrayIndex] <= 40 ){
                     gross = payRate[arrayIndex] * hoursWorked[arrayIndex];
                     regular = payRate[arrayIndex] * hoursWorked[arrayIndex];
                     overtime = 0;
                     }
                
                regularPay[arrayIndex]=regular;
                grossPay[arrayIndex]=gross;
                overtimePay[arrayIndex]=overtime;
               
                
                cout << "\nPayroll type = " << payrollType[arrayIndex] << endl;
                cout << "Regular pay = " << regularPay[arrayIndex] << endl;
                cout << "Overtime pay = " << overtimePay[arrayIndex] << endl;
                cout << "Gross pay = " << grossPay[arrayIndex] << endl;
                if (grossPay[arrayIndex] < 500)
                    state = 0.0;//if grossPay < 500, stateTax is 0
                else if (grossPay[arrayIndex] >= 1000)
                        state = grossPay[arrayIndex] * 0.05;
                        else
                        state = grossPay[arrayIndex] * 0.03;
                        
                        cout << "State tax = " << state << endl;
                        stateTax[arrayIndex]=state;
                        
                 if (grossPay[arrayIndex] < 500)
                    federal = 0.0;//if grossPay < 500, federalTax is 0
                 else if (grossPay[arrayIndex] >= 1000)
                        federal = grossPay[arrayIndex] * 0.07;
                        else
                        federal = grossPay[arrayIndex] * 0.05;
                        
                        cout << "Federal tax = " << federal << endl;
                        
                        federalTax[arrayIndex]=federal;
                        
                total = stateTax[arrayIndex] + federalTax[arrayIndex];//totalTax formula
                cout << "Total tax = " << total << endl;
                     totalTax[arrayIndex]=total;   
                

                
}
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
switch(unionCode[arrayIndex]){
                  case 1: unionDues[arrayIndex] = 15;
                  break;
                  case 2: unionDues[arrayIndex] = 25;
                  break;
                  case 3: unionDues[arrayIndex] = 35;
                  break;
                  }
                
                cout << "Union dues = " << unionDues[arrayIndex] << endl;

                netPay[arrayIndex] = grossPay[arrayIndex] - (totalTax[arrayIndex] + unionDues[arrayIndex]);
                
                cout << "Net pay = " << netPay[arrayIndex] << endl;
				return true;
}
double displayEmployeeResults(int empID[],int hoursWorked[],int payRate[],int unionCode[],char payrollType[],int arrayIndex,double regularPay[],double overtimePay[],double grossPay[],double netPay[],double stateTax[],double federalTax[],double totalTax[], double unionDues[]);
double displayEmployeeResults(int empID[],int hoursWorked[],int payRate[],int unionCode[],char payrollType[],int arrayIndex,double regularPay[],double overtimePay[],double grossPay[],double netPay[],double stateTax[],double federalTax[],double totalTax[], double unionDues[]){
          
          int i;
          
          cout <<"             Entry1       Entry2       Entry3       Entry4"<<endl;
          cout <<"Employee ID  ";
          for (i=0;i<4;i++){
              cout<<"             "<<empID[i]<<"            "<<empID[i+1];
              }
              cout<<endl;
          cout <<"Payroll Type ";
          for (i=0;i<4;i++){
              cout<<"             "<<payrollType[i];
              }
              cout<<endl;
          cout <<"Total Hours  ";
          for (i=0;i<4;i++){
              cout<<"             "<<hoursWorked[i];
              }
              cout<<endl;
          cout <<"Pay Rate     ";
          for (i=0;i<4;i++){
              cout<<"             "<<payRate[i];
              }
              cout<<endl;
          cout <<"Union Code   ";
          for (i=0;i<4;i++){
              cout<<"             "<<unionCode[i];
              }
              cout<<endl;
          cout <<"Regular Pay  ";
          for (i=0;i<4;i++){
              cout<<"             "<<regularPay[i];
              }
              cout<<endl;
          cout <<"Overtime Pay ";
          for (i=0;i<4;i++){
              cout<<"             "<<overtimePay[i];
              }
              cout<<endl;
          cout <<"Gross Pay    ";
          for (i=0;i<4;i++){
              cout<<"             "<<grossPay[i];
              }
              cout<<endl;
          cout <<"State Tax    ";
          for (i=0;i<4;i++){
              cout<<"             "<<stateTax[i];
              }
              cout<<endl;
          cout <<"Federal Tax  ";
          for (i=0;i<4;i++){
              cout<<"             "<<federalTax[i];
              }
              cout<<endl;
          cout <<"Total Tax    ";
          for (i=0;i<4;i++){
              cout<<"             "<<totalTax[i];
              }
              cout<<endl;
          cout <<"Union Dues   ";
          for (i=0;i<4;i++){
              cout<<"             "<<unionDues[i];
              }
              cout<<endl;
          cout <<"Net Pay      ";
          for (i=0;i<4;i++){
              cout<<"             "<<netPay[i];
              }
              cout<<endl;
			  return true;
}

double displayPayrollSummaryResults();

int main(int argc, char *argv[])
{
    int empID[4];
    int hoursWorked[4];
    int payRate[4];
    int unionCode[4];
    char payrollType[4];
    double unionDues[4];
    double regularPay[4];
    double overtimePay[4];
    double grossPay[4];
    double stateTax[4];
    double federalTax[4];
    double totalTax[4];
    double netPay[4];
    int arrayIndex =0;    
    char carryOn;
    
    
    cout << "Payroll Calculator" << endl;

    do {
    getInput(empID,hoursWorked,payRate,unionCode,payrollType,arrayIndex);

   doCalculations(empID,hoursWorked,payRate,unionCode,payrollType,arrayIndex,regularPay,grossPay,overtimePay,stateTax,federalTax,totalTax,unionDues,netPay);
    
          cout << "Would you like to calculate another employee's payroll? [Y/N]" << endl;
          cin >> carryOn;
          }
    while (carryOn == 'Y' || carryOn == 'y');
    
    while( carryOn != 'N' && carryOn != 'n' && carryOn != 'Y' && carryOn != 'y' ){
           cout<< "Invalid Entry. Would you like to calculate another employee's payroll? [Y/N]"<<endl;
           cin>> carryOn;
    if (carryOn == 'N' || carryOn == 'n'){
              displayEmployeeResults(empID,hoursWorked,payRate,unionCode,payrollType,arrayIndex,regularPay,grossPay,overtimePay,stateTax,federalTax,totalTax,unionDues,netPay);      
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}
Try that. Sorry, don't really know what I did. There was like 10 variables that were not being used, which I had no idea why. And you didn't end the last curly bracket, and 3 functions didn't return anything.

Don't know if it fixed your problem or not, hope so!
Kong, you are awesome! I was able to tweak this and get a working array.

One more question: how do I add some of the results to create totals and averages? I want to add all of the grossPay results to yield totalGrossPay, but I don't know how to pull info from individual cells. I tried totalGrossPay = grossPay[0]+grossPay[1]... etc. but that didn't work. To create an average, I would need to divide that totalGrossPay by totalEmployees, but I don't know how to count user entries either.

Sorry to bug everyone with this stuff on a Sunday :\
I got SO CLOSE to a working segment of code with:

1
2
3
4
5
6
    int i;
    for (i=0;i<arrayIndex;i++){
        totalGrossPay += grossPay[i];
    }
    cout<<"Total Gross Pay" <<totalGrossPay;


Only problem is, that for loop doesn't include the first employee in the totalGrossPay total.
Last edited on
Topic archived. No new replies allowed.