Arrays/Xcode

Hello all. I am trying to figure out this payroll program I am learning. The assignment is to use arrays in the first part (in order to learn them) and then functions in the second. I'm having trouble with understanding this section. I have posted my code below - it compiles but only shows the tab headings, no data. It is supposed to spit out the info for the data in the file I included below. Any help would be greatly appreciated!

JOHN SMITH 212-21-2121 M 50 10.00
JANE DOE 313-31-3131 S 45 19.00
SARA WHITE 414-41-4141 M 10 7.00
JAMES BROWN 515-51-5151 S 55 8.25



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

using namespace std;

int readindata(char firstname[][15], char lastname[][15], char taxstatus[][2], int employeeid[], float hoursworked[], float hourlyrate[], int counter);
void grosspaycalc (float hoursworked[], float hourlyrate[], float overtimehours[], float overtimepay[], float grospay[], int counter);
void taxongrosspay(float grosspay[], float taxrat[], int counter);
void taxonstatus(char taxstatus[][2], float grosspay[], float taxrate[], int counter);
void netpaycalc(float taxamount[], float grosspay[], float taxrate[], float netpay[], int counter);
void finaloutput(char firstname[][15], char lastname[][15], char taxstatus[][2], int employeeid[], float hoursworked[], float hourlyrate[], float overtimehours[], float overtimepay[], float grosspay[], float taxrate[], float taxamount[], float netpay[], int counter);

int main(){
const int MAXSIZE=100;
int employeeid[MAXSIZE];
float hourlyrate[MAXSIZE], taxamount[MAXSIZE], netpay[MAXSIZE], grosspay[MAXSIZE], hoursworked[MAXSIZE], overtimehours[MAXSIZE], overtimepay[MAXSIZE], taxrate[MAXSIZE];
char firstname[MAXSIZE][15], lastname[MAXSIZE][15], taxstatus[MAXSIZE][2];
int counter;
counter= readindata(firstname, lastname, taxstatus, employeeid, hoursworked, hourlyrate, MAXSIZE);
grosspaycalc(hoursworked, hourlyrate, overtimehours, overtimepay, grosspay, counter);
taxongrosspay(grosspay, taxrate, counter);
taxonstatus(taxstatus, grosspay, taxrate, counter);
netpaycalc(taxamount, grosspay, taxrate, netpay, counter);
finaloutput(firstname, lastname, taxstatus, employeeid, hoursworked, hourlyrate, overtimehours, overtimepay, grosspay, taxrate, taxamount, netpay, counter);

}

int readindata(char firstname[][15], char lastname[][15], char taxstatus[][2], int employeeid[], float hoursworked[], float hourlyrate[], int counter){

ifstream myfile;
myfile.open("/Users/Guest/Desktop/5b mod4 practice1/5b mod4 practice1/main.cpp");

counter=0;
while (myfile>>firstname[counter]>>lastname[counter]>>employeeid[counter]>>taxstatus[counter]>>hoursworked[counter]>>hourlyrate[counter]) {
counter=counter+1;
}

myfile.close();
return counter;
}

void grosspaycalc (float hoursworked[], float hourlyrate[], float overtimehours[], float overtimepay[], float grosspay[], int counter){

for (int i=0; i<counter; i=i+1) {
if (hoursworked[i]>40){
overtimehours[i]=hoursworked[i]-40;
overtimepay[i]=1.5*overtimehours[i]*hourlyrate[i];
grosspay[i]=overtimepay[i]+(hoursworked[i]*hourlyrate[i]);
}
else {
overtimepay[i]=0;
overtimehours[i]=0;
grosspay[i]=hourlyrate[i]*hoursworked[i];
}
}

}

void taxongrosspay(float grosspay[], float taxrate[], int counter){

for (int i=0; i<counter; i=i+1) {
if (grosspay[i]>1000) {
taxrate[i]=.30;
}

if (grosspay[i]>800 && grosspay[i]<=1000) {
taxrate[i]=.20;
}

if (grosspay[i]>500 && grosspay[i]<=800) {
taxrate[i]=.10;
}

if (grosspay[i]>=0 && grosspay[i]<=500) {
taxrate[i]=0;
}
}

}

void taxonstatus (char taxstatus[][2], float grosspay[], float taxrate[], int counter){

for (int i=0; i<counter; i=i+1) {

if ((((strcmp(taxstatus[i],"H")==0))||((strcmp(taxstatus[i], "h")==0)))&&(grosspay[i]>500)){
taxrate[i]=taxrate[i]-.05;
}

if (((strcmp(taxstatus[i],"S")==0))||((strcmp(taxstatus[i], "s")==0))){
taxrate[i]=taxrate[i]+.05;
}

if (((strcmp(taxstatus[i],"S")==0))||((strcmp(taxstatus[i], "s")==0))){
taxrate[i]=taxrate[i];
}
}
}

void netpaycalc(float taxamount[], float grosspay[], float taxrate[], float netpay[], int counter){

for (int i=0; i<counter; i=i+1) {
taxamount[i]=grosspay[i]*taxrate[i];
netpay[i]=grosspay[i]-taxamount[i];
}

}

void finaloutput (char firstname[][15], char lastname[][15], char taxstatus[][2], int employeeid[], float hoursworked[], float hourlyrate[], float overtimehours[], float overtimepay[], float grosspay[], float taxrate[], float taxamount[], float netpay[], int counter){

cout<<setw(55)<<"Melissa Inc. Payroll"<<endl;

cout<<setiosflags(ios::fixed|ios::showpoint|ios::left)<<setprecision(2);
cout<<setw(15)<<"First Name"<<setw(15)<<"Last Name"<<setw(15)<<"Tax Status"<<setw(15)<<"Employee ID"<<setw(15)<<"Hours Worked"<<setw(15)<<"Hourly Rate"<<setw(15)<<"Overtime Hours"<<setw(15)<<"Overtime Pay"<<setw(15)<< "Gross Pay"<<setw(15)<<"Tax Rate"<<setw(15)<<"Tax Amount"<<setw(15)<<"Net Pay"<<endl;

for (int i=0; i<counter; i=i+1) {
cout<<setw(15)<<firstname[i]<<setw(15)<<lastname[i]<<setw(15)<<taxstatus[i]<<setw(15)<<employeeid[i]<<setw(15)<<hoursworked[i]<<setw(15)<<hourlyrate[i]<<setw(15)<<overtimehours[i]<<setw(15)<<overtimepay[i]<<setw(15)<<grosspay[i]<<setw(15)<<taxrate[i]<<setw(15)<<taxamount[i]<<setw(15)<<netpay[i]<<endl;
}

}


Line 32: Why are you opening main.cpp?

I'm guessing your input operations at line 35 are failing trying to read data from a C++ source file. Therefore the counter at line 36 never gets incremented because you haven't read any employee data successfully.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Ok yes that was a stupid mistake. I corrected it to the file of employee data but it still isn't functioning.




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
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int readindata(char firstname[][15], char lastname[][15], char taxstatus[][2], int employeeid[], float hoursworked[], float hourlyrate[], int counter);
void grosspaycalc (float hoursworked[], float hourlyrate[], float overtimehours[], float overtimepay[], float grospay[], int counter);
void taxongrosspay(float grosspay[], float taxrat[], int counter);
void taxonstatus(char taxstatus[][2], float grosspay[], float taxrate[], int counter);
void netpaycalc(float taxamount[], float grosspay[], float taxrate[], float netpay[], int counter);
void finaloutput(char firstname[][15], char lastname[][15], char taxstatus[][2], int employeeid[], float hoursworked[], float hourlyrate[], float overtimehours[], float overtimepay[], float grosspay[], float taxrate[], float taxamount[], float netpay[], int counter);

int main(){
    const int MAXSIZE=100;
    int employeeid[MAXSIZE];
    float hourlyrate[MAXSIZE], taxamount[MAXSIZE], netpay[MAXSIZE], grosspay[MAXSIZE], hoursworked[MAXSIZE], overtimehours[MAXSIZE], overtimepay[MAXSIZE], taxrate[MAXSIZE];
    char firstname[MAXSIZE][15], lastname[MAXSIZE][15], taxstatus[MAXSIZE][2];
    int counter;
    counter= readindata(firstname, lastname, taxstatus, employeeid, hoursworked, hourlyrate, MAXSIZE);
    grosspaycalc(hoursworked, hourlyrate, overtimehours, overtimepay, grosspay, counter);
    taxongrosspay(grosspay, taxrate, counter);
    taxonstatus(taxstatus, grosspay, taxrate, counter);
    netpaycalc(taxamount, grosspay, taxrate, netpay, counter);
    finaloutput(firstname, lastname, taxstatus, employeeid, hoursworked, hourlyrate, overtimehours, overtimepay, grosspay, taxrate, taxamount, netpay, counter);
    
}//END MAIN

int readindata(char firstname[][15], char lastname[][15], char taxstatus[][2], int employeeid[], float hoursworked[], float hourlyrate[], int counter){
    
    ifstream myfile;
    myfile.open("/Users/Guest/Documents/payrollclass.txt");
    
    counter=0;
    while (myfile>>firstname[counter]>>lastname[counter]>>employeeid[counter]>>taxstatus[counter]>>hoursworked[counter]>>hourlyrate[counter]) {
        counter=counter+1;
    }//END WHILE
    
    myfile.close();
    return counter;
}//END READINDATA

void grosspaycalc (float hoursworked[], float hourlyrate[], float overtimehours[], float overtimepay[], float grosspay[], int counter){
    
    for (int i=0; i<counter; i=i+1) {
        if (hoursworked[i]>40){
            overtimehours[i]=hoursworked[i]-40;
            overtimepay[i]=1.5*overtimehours[i]*hourlyrate[i];
            grosspay[i]=overtimepay[i]+(hoursworked[i]*hourlyrate[i]);
        }//END IF
        else {
            overtimepay[i]=0;
            overtimehours[i]=0;
            grosspay[i]=hourlyrate[i]*hoursworked[i];
        }//END ELSE
    }//END FOR
    
}//END GROSSPAYCALC

void taxongrosspay(float grosspay[], float taxrate[], int counter){
    
    for (int i=0; i<counter; i=i+1) {
        if (grosspay[i]>1000) {
            taxrate[i]=.30;
        }//END IF
        
        if (grosspay[i]>800 && grosspay[i]<=1000) {
            taxrate[i]=.20;
        }//END IF
        
        if (grosspay[i]>500 && grosspay[i]<=800) {
            taxrate[i]=.10;
        }//END IF
        
        if (grosspay[i]>=0 && grosspay[i]<=500) {
            taxrate[i]=0;
        }//END IF
    }//END FOR
    
}//END TAXONGROSSPAY

void taxonstatus (char taxstatus[][2], float grosspay[], float taxrate[], int counter){
    
    for (int i=0; i<counter; i=i+1) {
        
        if ((((strcmp(taxstatus[i],"H")==0))||((strcmp(taxstatus[i], "h")==0)))&&(grosspay[i]>500)){
            taxrate[i]=taxrate[i]-.05;
        }//END IF
        
        if (((strcmp(taxstatus[i],"S")==0))||((strcmp(taxstatus[i], "s")==0))){
            taxrate[i]=taxrate[i]+.05;
        }//END IF
        
        if (((strcmp(taxstatus[i],"S")==0))||((strcmp(taxstatus[i], "s")==0))){
            taxrate[i]=taxrate[i];
        }//END IF
    }//END FOR
}//END TAXONSTATUS

void netpaycalc(float taxamount[], float grosspay[], float taxrate[], float netpay[], int counter){
    
    for (int i=0; i<counter; i=i+1) {
        taxamount[i]=grosspay[i]*taxrate[i];
        netpay[i]=grosspay[i]-taxamount[i];
    }//END FOR
    
}//END NETPAYCALC

void finaloutput (char firstname[][15], char lastname[][15], char taxstatus[][2], int employeeid[], float hoursworked[], float hourlyrate[], float overtimehours[], float overtimepay[], float grosspay[], float taxrate[], float taxamount[], float netpay[], int counter){
    
    cout<<setw(55)<<"Melissa Inc. Payroll"<<endl;
    
    cout<<setiosflags(ios::fixed|ios::showpoint|ios::left)<<setprecision(2);
    cout<<setw(15)<<"First Name"<<setw(15)<<"Last Name"<<setw(15)<<"Tax Status"<<setw(15)<<"Employee ID"<<setw(15)<<"Hours Worked"<<setw(15)<<"Hourly Rate"<<setw(15)<<"Overtime Hours"<<setw(15)<<"Overtime Pay"<<setw(15)<< "Gross Pay"<<setw(15)<<"Tax Rate"<<setw(15)<<"Tax Amount"<<setw(15)<<"Net Pay"<<endl;
    
    for (int i=0; i<counter; i=i+1) {
        cout<<setw(15)<<firstname[i]<<setw(15)<<lastname[i]<<setw(15)<<taxstatus[i]<<setw(15)<<employeeid[i]<<setw(15)<<hoursworked[i]<<setw(15)<<hourlyrate[i]<<setw(15)<<overtimehours[i]<<setw(15)<<overtimepay[i]<<setw(15)<<grosspay[i]<<setw(15)<<taxrate[i]<<setw(15)<<taxamount[i]<<setw(15)<<netpay[i]<<endl;
    }//END FOR
    
}//END FINALOUTPUT


Last edited on
You have been asked to use code tags. PLEASE DO SO.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
I will not respond further until you apply code tags.
SORRY! corrected.
Thanks for applying code tags.

The third field in your data file looks like a social security number (999-99-9999). You're trying to read that into an int (employeeid). Your input operation will only pick up the first three digits, leaving the buffer pointer at the "-". Your input operation is probably still failing since the data elements don't line up.

hmmmm ok so what would be the best way to correct this? redefine as a char?
That, or a std::string.
Also, I did a previous section of this assignment (I only have a screenshot at the moment so it'd take me a sec to locate the file and/or retype the code) where I did use char employeeid, but if I change this in the body of the code it starts causing a lot of other problems. Tips on how I could get this sorted out with minimal changes would be lovely if possible.
1
2
3
#include <string> 
// Replace line 16 with:
string employeeid[MAXSIZE];


Your read statement at line 35 does not need to change.



ok did that and now have "no matching function for call to 'readindata'" in line 20 and "no matching function of recall to 'finaloutput'" at line 25

not sure how to fix this
and would i need to change line 29 to

char employeeid
Since we changed the data type of employeid from int to string, the type of the employeid argument in those two functions needs to change from int to string in both the function and the corresponding protoype.

 
int readindata(char firstname[][15], char lastname[][15], char taxstatus[][2], string employeeid[], float hoursworked[], float hourlyrate[], int counter);




ok did that but still have a problem with "no matching function for call to 'readindata'" in line 20 and "no matching function of recall to 'finaloutput'" at line 25. This error didn't come up before making the above changes so there mud the something else I have to edit/add in those two sections to make this function.
Also, my teacher gave me the following code for the section using arrays but I can't get it to give me the desired output. Is there something I need to change to make it function in Xcode or is there another issue with the code? This is for the part that uses the arrays:
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
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main(){
    char empid[ 100 ][ 12 ];
    char fname[ 100 ][ 14 ],  lastname[ 100 ][ 15 ];
    int hw[ 100 ];
    double gp[ 100 ], np[ 100 ], hr[ 100 ], taxrate[100], taxamt[ 100 ];
    int counter  = 0;
    int i;
    cout<<"ENTER EMP ID, FNAME, LNAME, HRS WORKED, HRLY RATE ctrl z to end"<<endl;
    while( cin>>empid[counter]>>fname[counter]>>lastname[counter]>>hw[counter]>> hr[counter])
        counter=counter+1;
    for ( i=0; i<counter; i++){
        gp[i] = hw[i] * hr[i];}//end grosspay for loop
    for (i=0; i<counter; i++){
        if (gp[i]>500) taxrate[i] = .30;
        else if (gp[i]>200) taxrate[i]=.20;
        else taxrate[i] = .10;
        }// FOR
    for ( i=0; i<counter; i++){
        taxamt[i] = gp[i] * taxrate[i];}//end taxamount for loop
    for ( i=0; i<counter; i++){
        np[i] = gp[i] - taxamt[i];}//end netpay for loop
    cout<<endl;
    cout<<setw(14)<<"EMPLOYEE ID"<<setw(16)<<"FIRST  NAME"<<setw(17)
    <<"LAST NAME" <<setw(4)<<"HW"<<setw(5)<<"HR"<<setw(6)
    <<"GROSS"<<setw(6)<<"TAX"<<setw(9)<<"NET PAY"<<endl<<endl;
    for (i=0; i<counter; i++){
        cout<<setw(14)<<empid[i]<<setw(16)<<fname[i]<<setw(17)<<lastname[i]<<setw(4)
        <<hw[i]<<setw(5)<<hr[i]<<setw(6)<<gp[i]<<setw(6)<<taxamt[i]<<setw(9)<<np[i]<<endl;
        }// FOR
    return 0;
    }//MAIN 
I can't get it to give me the desired output.

First glance, the code looks reasonable.
What is the desired output?
What output are you getting?

edit: If you're getting only the header at line 28, I suspect your cin statement at 14 is failing. Note that the fields in this cin statement do NOT match the sample text file in your OP.

Last edited on
duh yes fixed that but still not getting anywhere with the 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
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main(){
    char empid[ 100 ][ 12 ];
    char fname[ 100 ][ 14 ],  lastname[ 100 ][ 15 ];
    int hw[ 100 ];
    double gp[ 100 ], np[ 100 ], hr[ 100 ], taxrate[100], taxamt[ 100 ];
    int counter  = 0;
    int i;
    cout<<"ENTER EMP ID, FNAME, LNAME, HRS WORKED, HRLY RATE ctrl z to end"<<endl;
    while( cin>>fname[counter]>>lastname[counter]>>empid[counter]>>hw[counter]>> hr[counter])
        counter=counter+1;
    for ( i=0; i<counter; i++){
        gp[i] = hw[i] * hr[i];}//end grosspay for loop
    for (i=0; i<counter; i++){
        if (gp[i]>500) taxrate[i] = .30;
        else if (gp[i]>200) taxrate[i]=.20;
        else taxrate[i] = .10;
        }// FOR
    for ( i=0; i<counter; i++){
        taxamt[i] = gp[i] * taxrate[i];}//end taxamount for loop
    for ( i=0; i<counter; i++){
        np[i] = gp[i] - taxamt[i];}//end netpay for loop
    cout<<endl;
    cout<<setw(14)<<"EMPLOYEE ID"<<setw(16)<<"FIRST  NAME"<<setw(17)
    <<"LAST NAME" <<setw(4)<<"HW"<<setw(5)<<"HR"<<setw(6)
    <<"GROSS"<<setw(6)<<"TAX"<<setw(9)<<"NET PAY"<<endl<<endl;
    for (i=0; i<counter; i++){
        cout<<setw(14)<<empid[i]<<setw(16)<<fname[i]<<setw(17)<<lastname[i]<<setw(4)
        <<hw[i]<<setw(5)<<hr[i]<<setw(6)<<gp[i]<<setw(6)<<taxamt[i]<<setw(9)<<np[i]<<endl;
        }// FOR
    return 0;
    }//MAIN 


my output is just:
ENTER EMP ID, FNAME, LNAME, HRS WORKED, HRLY RATE ctrl z to end

but this doesn't do anything when I enter the information.

Also, still haven't figured out the errors in the longer code from above in previous messages. ugh. my teacher takes forever to respond to messages so haven't been able to get any answers from him
ps i adjusted the input file so that it reads properly - last name, first name, id, hrs worked, rate. Whoops just realized i didn't change the cout statement either.

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
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main(){
    char empid[ 100 ][ 12 ];
    char fname[ 100 ][ 14 ],  lastname[ 100 ][ 15 ];
    int hw[ 100 ];
    double gp[ 100 ], np[ 100 ], hr[ 100 ], taxrate[100], taxamt[ 100 ];
    int counter  = 0;
    int i;
    cout<<"ENTER EMP ID, FNAME, LNAME, HRS WORKED, HRLY RATE ctrl z to end"<<endl;
    while( cin>>fname[counter]>>lastname[counter]>>empid[counter]>>hw[counter]>> hr[counter])
        counter=counter+1;
    for ( i=0; i<counter; i++){
        gp[i] = hw[i] * hr[i];}//end grosspay for loop
    for (i=0; i<counter; i++){
        if (gp[i]>500) taxrate[i] = .30;
        else if (gp[i]>200) taxrate[i]=.20;
        else taxrate[i] = .10;
        }// FOR
    for ( i=0; i<counter; i++){
        taxamt[i] = gp[i] * taxrate[i];}//end taxamount for loop
    for ( i=0; i<counter; i++){
        np[i] = gp[i] - taxamt[i];}//end netpay for loop
    cout<<endl;
    cout<<setw(16)<<"FIRST  NAME"<<setw(17)
    <<"LAST NAME" <<setw(14)<<"EMPLOYEE ID"<<setw(4)<<"HW"<<setw(5)<<"HR"<<setw(6)
    <<"GROSS"<<setw(6)<<"TAX"<<setw(9)<<"NET PAY"<<endl<<endl;
    for (i=0; i<counter; i++){
        cout<<setw(14)<<empid[i]<<setw(16)<<fname[i]<<setw(17)<<lastname[i]<<setw(4)
        <<hw[i]<<setw(5)<<hr[i]<<setw(6)<<gp[i]<<setw(6)<<taxamt[i]<<setw(9)<<np[i]<<endl;
        }// FOR
    return 0;
    }//MAIN 
Topic archived. No new replies allowed.