Hi, I am trying to undertake an exercise in which a payroll file is generated. Unfortunately, I seem to be having problems with respect to passing values to functions that calculate pay for various types of employees and tax rates. The code compiles and runs, but generates zeroes for the various calculations that take place in the functions. Any advice or help would be deeply appreciated.

I have included a file, as that might make my code easier to read that the pasted code below. Thanks again, kindly.

#include<iostream>
#include<fstream>
#include<iomanip>
#include<math.h>
#include<string>
using namespace std;

const int Count = 1, Size1 = 10, Size2 = 15;
const float stateTaxRate = 0.05;
const float fedTaxRate = 0.15;
const float unionFees = 0.02;

float calcManagerGross(float);
float calcLevelOneGross(float);
float calcLevelTwoGross(float);
float calcAllOthersGross(float);
float calcStateTax(float);
float calcFederalTax(float);
float calcUnionFees(float);
float calcNet(float);
void displayResults(void);

typedef char List_A[Count][Size1];
typedef char List_B[Count][Size2];

int i,j;
float hours[Count], rate[Count], net[Count], taxAmt[Count], gross[Count], overTime[Count], stateTaxAmt[Count], fedTaxAmt[Count], unionFeeAmt[Count];
float managerGross, levelOneGross, levelTwoGross, allOthersGross, stateTax, fedTax, employeeNet;
List_A First_Name;
string middleInitial;
List_B Last_Name;
List_B ID_Num;
char selection;

int main()
{

    ofstream myfile;
    myfile.open("payrecord.txt");
    
    
    for(i=0; i<Count; i++)
    {       
            cout << "Please enter the employee's first name: " << endl;
            cin >> First_Name[i];
            cout << "Please enter the employee's middle initial: " << endl;
            cin >> middleInitial[i];
            cout << "Please enter the employee's last name: " << endl;
            cin >> Last_Name[i];
            cout << "Please enter the employee ID number: " << endl;
            cin >> ID_Num[i];
            cout << "Please enter the employee's compensation level: " << endl;
            cout << '\t' << "A. Manager" << endl;
            cout << '\t' << "B. Work Level I" << endl;
            cout << '\t' << "C. Work Level II" << endl;
            cout << '\t' << "D. All others" << endl;
            cin >> selection;
            switch(selection)
            {
                            case 'A':
                            cout << "Please enter the total hours of *OVERTIME* that employee worked. Company " << endl;
                            cout << "policy dictates that the minimum weekly hours is 40 and the maximum is" << endl;
                            cout << "60 for compensation purposes, so please enter only hours of overtime." <<  endl;
                            cin >> overTime[i];
                            if(overTime[i]<0 || overTime[i]>20)
                            {
                                 do
                                 {
                                      cout << "You can only enter between 0 and 20 hours of overtime per week." << endl;
                                      cin >> overTime[i];
                                 }
                                 while(overTime[i]<0 || overTime[i]>20);
                            }
                            cout << "Please enter the employee's hourly rate: " << endl;
                            cin >> rate[i];
                            if(rate[i]<=0)
                            {
                                 do
                                 {
                                      cout << "Please enter a positive, nonzero number." << endl;
                                      cin >> rate[i];
                                 }
                                 while(rate[i]<=0);
                            }
                            gross[i]=calcManagerGross(managerGross);
                            break;

                            case 'B':
                            cout << "Please enter the total hours of *OVERTIME* that employee worked. Company " << endl;
                            cout << "policy dictates that the minimum weekly hours is 40 and the maximum is" << endl;
                            cout << "60 for compensation purposes, so please enter only hours of overtime." <<  endl;
                            cin >> overTime[i];
                            if(overTime[i]<0 || overTime[i]>20)
                            {
                                 do
                                 {
                                      cout << "You can only enter between 0 and 20 hours of overtime per week." << endl;
                                      cin >> overTime[i];
                                 }
                                 while(overTime[i]<0 || overTime[i]>20);
                            }
                            cout << "Please enter the employee's hourly rate: " << endl;
                            cin >> rate[i];
                            if(rate[i]<=0)
                            {
                                 do
                                 {
                                      cout << "Please enter a positive, nonzero number." << endl;
                                      cin >> rate[i];
                                 }
                                 while(rate[i]<=0);
                            }
                            gross[i]=calcLevelOneGross(levelOneGross);
                            break;

                            case 'C':
                            cout << "Please enter the total hours of *OVERTIME* that employee worked. Company " << endl;
                            cout << "policy dictates that the minimum weekly hours is 40 and the maximum is" << endl;
                            cout << "60 for compensation purposes, so please enter only hours of overtime." <<  endl;
                            cin >> overTime[i];
                            if(overTime[i]<0 || overTime[i]>20)
                            {
                                 do
                                 {
                                      cout << "You can only enter between 0 and 20 hours of overtime per week." << endl;
                                      cin >> overTime[i];
                                 }
                                 while(overTime[i]<0 || overTime[i]>20);
                            }
                            cout << "Please enter the employee's hourly rate: " << endl;
                            cin >> rate[i];
                            if(rate[i]<=0)
                            {
                                 do
                                 {
                                      cout << "Please enter a positive, nonzero number." << endl;
                                      cin >> rate[i];
                                 }
                                 while(rate[i]<=0);
                            }
                            gross[i]=calcLevelTwoGross(levelTwoGross);
                            break;

                            case 'D':
                            cout << "Please enter the total hours of *OVERTIME* that employee worked. Company " << endl;
                            cout << "policy dictates that the minimum weekly hours is 40 and the maximum is" << endl;
                            cout << "60 for compensation purposes, so please enter only hours of overtime." <<  endl;
                            cin >> overTime[i];
                            if(overTime[i]<0 || overTime[i]>20)
                            {
                                 do
                                 {
                                      cout << "You can only enter between 0 and 20 hours of overtime per week." << endl;
                                      cin >> overTime[i];
                                 }
                                 while(overTime[i]<0 || overTime[i]>20);
                            }
                            cout << "Please enter the employee's hourly rate: " << endl;
                            cin >> rate[i];
                            if(rate[i]<=0)
                            {
                                 do
                                 {
                                      cout << "Please enter a positive, nonzero number." << endl;
                                      cin >> rate[i];
                                 }
                                 while(rate[i]<=0);
                            } 
                            gross[i]=calcAllOthersGross(allOthersGross);
                            break;

                            default:
                            do
                            {
                                 cout << "Please enter one of the above menu choices." << endl;
                                 cin >> selection;
                            }
                            while((selection != 'A') && (selection !='B') && (selection != 'C') && (selection != 'D'));
           } // end of switch structure
           cout << "Your gross is " << gross << endl;
           stateTaxAmt[i] = calcStateTax(stateTax);
           fedTaxAmt[i] = calcFederalTax(fedTax);
           unionFeeAmt[i] = calcUnionFees(unionFees);
           net[i] = calcNet(employeeNet);
}
           
displayResults();

myfile.close();

system("pause");
return 0;

} // end of main

//function CALC MANAGER GROSS

float calcManagerGross(float)
{
     float rate, gross; 
     gross = 40*(rate); 
     return(gross);    
}

//function CALC LEVEL ONE GROSS

float calcLevelOneGross(float)
{
     float overTime, rate, gross;
     gross = (overTime*(1.5*rate))+(40*rate);  
     return(gross);

}
//function CALC LEVEL TWO GROSS

float calcLevelTwoGross(float)
{
     float overTime, rate, gross;
     if(overTime<=15)
     {
         gross = (overTime*(1.5*rate)+(40*rate));
     }
     else
     {
         gross = (15*1.5*(rate)+(40*rate));
     }
     return(gross);
} 

//function CALC OTHERS GROSS

float calcAllOthersGross(float)
{
     float overTime, rate, gross;
     if(overTime<=10)
     {
         gross = (overTime*(1.5*rate)+(40*rate));
     }
     else
     {
         gross = (10*1.5*(rate)+(40*rate));
     }
     return(gross);
}

//function CALC STATE TAX

float calcStateTax(float stateTax)
{
    float gross, stateTaxAmt;
    stateTaxAmt = gross - (gross*stateTaxRate);
    return(stateTaxAmt);
} 

//function CALC FED TAX

float calcFederalTax(float fedTax)
{
    float gross, fedTaxAmt;
    fedTaxAmt = gross - (gross*fedTaxRate);
    return(fedTaxAmt);
}
          
//function CALC UNION FEES

float calcUnionFees(float unionFee)
{
    float unionFeeAmt, gross;
    unionFeeAmt = gross - (gross*unionFees);
    return(unionFeeAmt);
}

//function CALC NET

float calcNet(float totalNet)
{
     float net, gross, stateTaxAmt, fedTaxAmt, unionFeesAmt;
     net = gross - stateTaxAmt - fedTaxAmt - unionFeesAmt;
     return(net);
}
         

//function DISPLAY RESULTS

void displayResults(void)
{
     for(j=0; j<Count; j++)
     {
            cout << First_Name[j] << '\t' << setw(1) << middleInitial[j] << '\t' << setw(1) << Last_Name[j] << '\t' << setw(1) << ID_Num[j] << '\t' << setw(1) << setprecision(2) << fixed << rate[j] << '\t' << setw(1) << setprecision(2) << fixed << overTime[j] << '\t' << setw(1) << setprecision(2) << fixed << "$" << gross[j] << '\t' << setw(1) << setprecision(2) << fixed << "$" << stateTaxAmt[j] << '\t' << setw(1) << setprecision(2) << fixed << "$" << fedTaxAmt[j] << '\t' << setw(1) << setprecision(2) << fixed << "$" << unionFeeAmt[j] << '\t' << setw(1) << setprecision(2) << fixed << "$" << net[j] << endl;
     }
}
gerard4143 commented: When you write code, run it often checking for proper results. -3

Recommended Answers

All 6 Replies

Maybe you could isolate one function and that function call that produces the zeros and post that...Posting ~ three hundred lines of code and saying it doesn't work - fix it please - is infuriating.

Add #include <cstdlib> at the top.
Tip: Next time you have a problem, try to write the error messages you have, and if it is possible, isolate the part of the program where you get the error.

There are no error messages; as I said in the initial thread header, it compiles and runs.

Try reading how to create function in C++

You have this.

float calcManagerGross(float)//??????float what?
{
     float rate, gross; 
     gross = 40*(rate); 
     return(gross);    
}

How could you write ~ 300 lines of code and not once check if the code was producing correct results?

After going through this it is clear that you do not understand a few things.

#1 you have to name your arguments/parameters for your functions (except for the function proto-types)
#2 you are using uninitialized variables in calculations (this is why you are getting 0)
#3 you are not passing the right variables into your functions and you are not using the passed variables within your function

Even if you did pass the correct variables in and there were no other problems, your formula for calculating the union fees and government taxes is incorrect (it should be gross*taxrate).

I have attached a revised copy that you should compare side by side with your original. The primary changes are where you call your calculation functions and your function definitions.

Edit: You should take a look at this page on functions.

Thanks for the help, I will take a look at this. And for the record, yes, it should be obvious that there are things that I don't understand about arrays, passing to functions, etc: I'm a student, and learning them now. Also, if I understood them well, I wouldn't need to ask for help. But, I appreciate the guidance and will follow up on the links and help offered. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.