Function issues in a payroll program.

Using Dev C++. The Program works with one minor problem. The fname and the lname comes out blank. here is the code.

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

//functions
void findovt(int[], int[], float[],float[], float[], int);
void findgp(float[], float[], float[], int);
void findtaxr(float[], float[], char[], int);
void findtax(float[], float[], float[], int);
void findnp(float[], float[], float[], int);
void outputall(char[], char[], int[], int[], float[], char[], int[], float[],
               float[], float[], float[], float[], int);

int main(){

     
 //declaration of variables

 int id[100], hw[100], oth[100];
 float hr[100], otp[100], rgp[100], gp[100], tax[100],
       np[100], TAXR[100];
 char fname[100][15], lname[100][15], ms[100];
 int i, n=0; 

  ifstream fin("payrollfunc.txt");
  cout<<setiosflags(ios::left)<<"                                Digdug's National Bank"<<endl<<endl;
  cout<<setiosflags(ios::left)<<setw(15)<<"FIRST NAME"<<setw(15)<<"LAST NAME"<<setw(7)<<"EMP ID"<<setw(4)<<"HW"<<setw(6)<<"HR"<<setw(5)<<"STAT"<<setw(5)<<"OTH"<<setw(7)<<"OTP"<<setw(7)<<"REGP"<<setw(9)<<"GROSS"<<setw(7)<<"TAX"<<setw(7)<<"NET"<<endl<<endl;
  cout<<setiosflags(ios::left)<<setw(15)<<"=============="<<setw(15)<<"=============="<<setw(7)<<"======"<<setw(4)<<"==="<<setw(6)<<"====="<<setw(5)<<"===="<<setw(5)<<"==="<<setw(7)<<"======"<<setw(7)<<"======"<<setw(9)<<"========"<<setw(7)<<"======"<<setw(7)<<"======"<<endl;
  while(fin>>fname[n]>>lname[n]>>id[n]>>hw[n]>>hr[n]>>ms[n]) n++;
//Function Call
 findovt(hw, oth, hr, otp, rgp, n);
 findgp(rgp, otp, gp, n);
 findtaxr(gp, TAXR, ms, n);
 findtax(gp, tax, TAXR, n);
 findnp(gp, np, tax, n);
 outputall(fname[15], lname[15], id, hw, hr, ms, oth, otp, rgp, gp, tax, np, n);
 system("pause");
 return 0;
}//MAIN 
// function definitions


void findovt(int hw[], int oth[], float hr[], float otp[], float rgp[], int n){
  for(int i=0;i<n;i++){
      if(hw[i]>40){
      oth[i]=hw[i]-40;
      otp[i]=oth[i]*hr[i]*1.5;
      rgp[i]=hw[i]*hr[i];
    }//IF
    else{
     oth[i]=0;
     otp[i]=0;
     rgp[i]=hw[i]*hr[i];
    }//ELSE
  }//FOR
}//FINDOVT


void findgp(float rgp[], float otp[], float gp[], int n){
  for(int i=0;i<n;i++){
    gp[i]=rgp[i]+otp[i];
  }//FOR
}//FINDGP

void findtaxr(float gp[], float TAXR[], char ms[], int n){
  for(int i=0;i<n;i++){
   if ((gp[i]>1000) && ms[i]=='S'||ms[i]=='s') TAXR[i]=0.35;
   else if ((gp[i]>1000) && ms[i]=='M'||ms[i]=='m') TAXR[i]=0.30;
   else if ((gp[i]>1000) && ms[i]=='H'||ms[i]=='h') TAXR[i]=0.25;
   else if ((gp[i]>800) && ms[i]=='S'||ms[i]=='s') TAXR[i]=0.25;
   else if ((gp[i]>800) && ms[i]=='M'||ms[i]=='m') TAXR[i]=0.20;
   else if ((gp[i]>800) && ms[i]=='H'||ms[i]=='H') TAXR[i]=0.15;
   else if ((gp[i]>500) && ms[i]=='S'||ms[i]=='s') TAXR[i]=0.15;
   else if ((gp[i]>500) && ms[i]=='M'||ms[i]=='m') TAXR[i]=0.10;
   else if ((gp[i]>500) && ms[i]=='H'||ms[i]=='h') TAXR[i]=0.05;
   else TAXR[i]=0.0;
  }//FOR
}//FINDTAXR

void findtax(float gp[], float tax[], float TAXR[], int n){
  for(int i=0;i<n;i++){
     tax[i]=gp[i]*TAXR[i];
  }//FOR
}//FINDTAX

void findnp(float gp[], float np[], float tax[], int n){
  for(int i=0;i<n;i++){
     np[i]=gp[i]-tax[i];
  }//FOR
}//FINDNP

void outputall(char fname[], char lname[], int id[], int hw[], float hr[],
     char ms[], int oth[], float otp[], float rgp[], float gp[], float tax[],
     float np[], int n){
   for(int i=0;i<n;i++){
  cout<<fixed;
   cout<<setiosflags(ios::showpoint|ios::fixed|ios::left)<<setw(15)<<fname[i]
       <<setw(15)<<lname[i]<<setw(7)<<id[i]<<setw(4)<<setprecision(1)<<hw[i]
       <<setw(6)<<setprecision(2)<<hr[i]<<setw(5)<<ms[i]<<setw(5)
       <<setprecision(1)<<oth[i]<<setw(7)<<setprecision(2)<<otp[i]<<setw(9)
       <<rgp[i]<<setw(9)<<gp[i]<<setw(7)<<tax[i]<<setw(7)<<np[i]<<endl;
  }//FOR

}//OUTPUTALL
//end 


Does anyone see my mistake.
Last edited on
Not sure if I'm looking at this right but in the line :

while(fin>>fname[i]>>lname[i]>>id[n]>>hw[n]>>hr[n]>>ms[n]) n++;

Don't you want fname[n]>>lname[n] ?
fname and lname I did switch back to [n], I switch it to i to see if it would correct the missing names but it didn't work as well.
This may not be all of the problem, but, you are missing the # in your line:

include<iostream>. It should read #include<iostream>
Iforgot to copy the #, sorry.
No sorries.......here is my code that worked:

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


   char firstname[100][10], lastname [100][15], status[100];
   int empid[100], i=0, n=0;
   float overtimehours[100], regularhours[100], hoursworked[100];
   float hourlyrate[100], overtimerate[100], taxamount[100], taxrate[100];
   double grosspay[100], overtimepay[100], regularpay[100], netpay[100];
    
    //begin findovertime hours
    void findovertimehours(void){                
                        for(i=0;i<n;i++){                              
                        if(hoursworked[i]>40){
       overtimehours[i]=hoursworked[i]-40;
       overtimerate[i]=overtimehours[i]*hourlyrate[i]*1.5;
       }//End if
       else{
       overtimehours[i]=0;
       overtimerate[i]=0;
       }//End if
       }//End for
       }//end findovertime hours
       
       //begin payroll calculation
       void findpayrollcalculation(void){
       for(i=0;i<n;i++){     
       regularpay[i]=40*hourlyrate[i];
       if(overtimehours[i]>0)
       overtimepay[i]=overtimehours[i]*overtimerate[i];
       grosspay[i]=regularpay[i]+overtimepay[i];
       }//End if
       }//end payroll calculation
       
       //begin taxrate calculation
       void taxratecalculation(void){
          for(i=0;i<n;i++){
                        if(grosspay[i]>1000) taxrate[i]=0.30;
                        else if(grosspay[i]>800) taxrate[i]=0.20;
                        else if(grosspay[i]>500) taxrate[i]=0.10;
                        else taxrate[i]=0.0;
          
                   if (status[i]=='S') taxrate[i]=(taxrate[i]+.05);
       else if (status[i]=='s') taxrate[i]=(taxrate[i]+.05);
       else if (status[i]=='H' && grosspay[i]>500) taxrate[i]=(taxrate[i]-.05);
       else if (status[i]=='h' && grosspay[i]>500) taxrate[i]=(taxrate[i]-.05);
       else if (status[i]=='M') taxrate[i]=(taxrate[i]*1);
       else if (status[i]=='m') taxrate[i]=(taxrate[i]*1);
                                }//End for
                        for(i=0;i<n;i++){
            taxamount[i]=grosspay[i]*taxrate[i];
            netpay[i]=grosspay[i]-taxamount[i];
            }//End of Payroll calculations
            }//end taxrate calculation
            
            int main (){
                int findovertimehours();
                int findpayrollcalculation();
                int findtaxratecalculation();
                std::ifstream fin("ebrahimipayroll.in");
                while(fin>>firstname[n]>>lastname[n]>>empid[n]>>status[n]>>
                hoursworked[n]>>hourlyrate[n])n++;
                //begin call functions
                findovertimehours();
                findpayrollcalculation();
                taxratecalculation();
                //end call functions

      std::cout<<std::setw(58)<<"\n\n\n\t\tDR. EBRAHIMI'S PAYROLL INSTITUTE"<<
      std::endl<<std::endl<<std::endl;
                               
      std::cout<<std::setw(8)<<std::left<<"FIRST"<<std::setw(9)<<"LAST"
      <<std::setw(6)<<"ID"<<std::setw(6)<<"STAT"<<std::setw(4)<<"HW"<<
      std::setw(5)<<"HR"<<std::setw(4)<<"OTH"<<std::setw(6)<<"OTP"<<
      std::setw(8)<<"REGP"<<std::setw(8)<<"GROSS"<<std::setw(6)<<"TAX"<<
      std::setw(8)<<"NET"<<std::endl;
      std::cout<<std::setw(8)<<std::left<<"====="<<std::setw(9)<<"===== "<<
      std::setw(6)<<"===="<<std::setw(6)<<"===="<<std::setw(4)<<"==="<<
      std::setw(5)<<"==="<<std::setw(4)<<"==="<<std::setw(6)<<"==="<<
      std::setw(8)<<"====="<<std::setw(8)<<"====="<<std::setw(6)<<"===="
      <<std::setw(3)<<"====="<<std::endl;
            
      //Output of employee payroll calculations
      for(i=0; i<n;i++){      
      
      
      std::cout<<std::setw(8)<<firstname[i]<<std::setw(9)<<lastname[i]<<std::setw(6)
      <<empid[i]<<std::setw(6)<<status[i]<<std::setw(4)<<hoursworked[i]
      <<std::setw(5)<<hourlyrate[i]<<std::setw(4)<<overtimehours[i]<<std::setw(6)
      <<overtimepay[i]<<std::setw(8)<<regularpay[i]<<std::setw(8)
      <<grosspay[i]<<std::setw(6)<<taxrate[i]<<std::setw(1)<<"$"<<netpay[i]
      <<std::endl<<std::endl;    
                         
                                            }//FOR               

                               system ("pause");
                               return 0;
                               }//MAIN
                               
    
I don't think that code worked... are you sure that's the right stuff??
I wonder if the # of functions has something to do with it? hmmmm
I'm not sure how to pass char arrays to a function but that appears to be where your trouble is.

If you take the code for outputall and put it into main in place of the function call your program seems to output correctly. At least with only one data item in the test file I made.
Yes, the code worked perfectly. I am using Dev C++ 4.9.9.2 as the compiler.
I'm using dev C++ as well 4.9.9.2. Lost are you talking about my orig or with the cout in the main?
OK - in the one you say worked I was questioning the following lines:
1
2
3
4
            int main (){
                int findovertimehours();
                int findpayrollcalculation();
                int findtaxratecalculation();
It would never have compiled written like that.
Once those lines are commented out then the file read will work like you say.

I have also noticed that in the code you said worked you are reading the file as firstname, lastname,ID, status,hoursworked, hourlypay ,

but in the first code you posted you are reading the file lines as
firstname, lastname, id, hoursworked, hourlypay, status

have you done something with the file layout design??
Sorry Gulkan. But you are looking at two different codes with similar subjects.
Gulkan.......It did compile......this is the output:




DR. EBRAHIMI'S PAYROLL INSTITUTE


FIRST LAST ID STAT HW HR OTH OTP REGP GROSS TAX NET
===== ===== ==== ==== === === === === ===== ===== ==== =====
John Smith 2222 M 50 15 10 2250 600 2850 0.3 $1995

Jane Dow 3333 M 45 10 5 375 400 775 0.1 $697.5

Holly Sorrell 4444 M 40 25 0 0 1000 1000 0.2 $800

Mary Adams 5555 S 30 15 0 0 600 600 0.15 $510

Kyle Mustard 6661 M 42 10 2 60 400 460 0 $460

Press any key to continue . . .



It looks better on the print screen...but, that's the output.
Is that mine or is that loststudents program?
Thanks Lost, your program gave some ideas to fix my error. It works just fine now. The void function(void) I was the key. I take it your in the same class, so if you want to email through the class, last name is Rigdon.
Topic archived. No new replies allowed.