calcPay

Could someone please help me? I have been working on this code for about 3 weeks now. I have had help from a tutor at our school and he was not able to help me. It is a homework assignment that I am trying to accomplish. Once I know how to call the appropriate function, I should be able to do the next 3 assignments without a problem. I am not looking for the answer, just a direction that I need to move in to write the code properly. I am having issues with the code not calculating the total gross pay for the 4 weeks of pay that is calculated. Any help will be greatly appreciated.

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

using namespace std;                                       

//global variables
double hours;                                              //hours worked
double rate;                                               //rate of pay
double regPay;                                             //regular hours worked pay
double otPay;                                              //overtime pay
double totalGrossPay;
int week;
int i = 0;                                            

//function prototypes
void calculateWeek(int, int);                              //calculates the weeks needed
double calculateRegPay(double, double);                    //calculates regular pay
double calculateOtPay(double, double, double);             //calculates overtime pay
double calculateTotalPay(double, double, double);          //calculates the total gross pay for the month

//main function
int main()
{
calculateWeek(i, week);

//if statement
    if (hours <= 40)
    {
        double regPay = calculateRegPay(hours, rate);                         //calling the function for calculating the regular hours pay
        //cout << "The gross pay for this week is $" << regPay;
    }
    else if (hours > 40)
    {
        double otPay = calculateOtPay(hours, rate, otPay);                    //calling the function for calculating the overtime pay
       return 0; 
    }
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "%%%  The gross pay for this week is $" << setprecision(2) << fixed << otPay << " %%%" << endl;
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
//end the if statement

//call a function
    double totalGrossPay = calculateTotalPay (regPay, otPay, totalGrossPay);  //calls function to calculate the total pay for the month

    return 0;
}
//end main function

//function week calculation definition
void calculateWeek(int i, int week)
{
    for (i = 0; i<4; ++i) {

		cout << "Enter info for week " << (i + 1) << endl;
		cout << "***************************" << endl;
		cout << endl;

		cout << "Enter hours worked or 0 to : ";
		cin >> hours;
		cout << endl;
		while (hours != 0)
    {
        cout << "Enter hourly rate for the employee using this format (00.00): ";
        cin >> rate;
cout << "\nEnter hours worked or 0 to exit: ";
        cin >> hours;
        cout << endl;
    }}
}

//function regular pay definition
     double calculateRegPay (double hours, double rate)
    {
         regPay = hours * rate;
         return regPay;
    }
//end function

//function overtime pay definition
    double calculateOtPay(double hours, double rate, double otPay)
   {
       otPay = (hours > 40);
       otPay = (hours - 40) * (rate * 1.5) + (rate * 40);
       return otPay;
   }
//end function

//function total gross pay definition
   double calculateTotalPay(double regPay, double otPay, double totalGrossPay)
   {
       totalGrossPay += (regPay + otPay);
       return totalGrossPay;
   }
//end function definition
Line 2: setprecision should be a comment

Line 34: You're creating a new instance of otPay which goes out of scope at line 36.

Line 35: Why are you exiting the program if hours > 40?

Line 38: otPay is an uninitialized variable.

Line 37-39: Why are you displaying gross pay here, when you don't calculate it until line 43.

Line 43: You're creating a new instance of totalGrossPay, which immediately goes out of scope.

Line 50: Why are you passing i as an argument. It should be a local variable.

Line 59,64: You're modifying global variables inside a loop. Only the last value will be saved.

Line 74: Why are you modifying a global variable, then returning it? regPay should be a local variable. Global variables should be avoided.

Line 80: Why is otPay passed as an argument?

Line 82: What is this line supposed to be doing? It's a boolean expression setting otPay to 1 or 0, then being wiped out on the next line.

Line 89: Why is totalGrossPay an argument? It should be a local variable.


I am a beginner in C++. I will look at these lines of interest and try to figure it out. A lot of this information that you gave me, I do not understand but I will try to figure it out. Thank you for your help.
I still do not understand this code. When I first wrote the code without a custom function it worked great. Since my instructor told me to use the custom function calculatePay(), I have been stuck ever since then. I do not understand how to call the custom function for calculating all 4 weeks of pay. I am at my witts end! Please help me to understand these custom functions more. I have been reading books and searching the web but I still have not found the answer that I need. All help will be greatly appreciated.
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
#include <iostream>                                       
#include <iomanip>                                         

using namespace std;                                       

struct Week                                             //  A structure to store one week's data
{   int     hours;
    double  rate;
};

static const int MAX_WEEKS = 4;                         //  Number of weeks in a month
    
// Removed all globals 

//function prototypes
int calculateWeek (Week weeks[]);                       //  Input data for up to 4 weeks
double calculateRegPay (Week);                          //calculates regular pay
double calculateOtPay (Week);                           //calculates overtime pay
double calculateTotalPay(double, double);               //calculates the total gross pay 

//main function
int main()
{   Week    weeks[MAX_WEEKS];                           //  4 weeks worth of data
    int     weeks_worked;
    double  regPay = 0;
    double  otPay = 0;
    double  totalGrossPay = 0;
    
    weeks_worked = calculateWeek (weeks);
    //  Calculate pay for each week worked
    for (int i=0; i<weeks_worked; i++)
    {   if (weeks[i].hours <= 40)
            regPay += calculateRegPay (weeks[i]);                       
        else          
            otPay += calculateOtPay (weeks[i]);                  
        totalGrossPay += calculateTotalPay (regPay, otPay);     //  Add week's gross to total
    }        
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "%%%  The gross pay is $" << setprecision(2) << fixed << totalGrossPay << " %%%" << endl;
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    return 0;
}

//  Input hours and rate for up to 4 weeks 
//  Returns weeks worked.
int calculateWeek (Week weeks[])
{   for (int i=0; i<MAX_WEEKS; ++i) 
    {   cout << "Enter info for week " << (i + 1) << endl;
		cout << "***************************" << endl;
		cout << endl;

		cout << "Enter hours worked or 0 to : ";
		cin >> weeks[i].hours;
		cout << endl;
		if (weeks[i].hours == 0)
		    return i;   //  Number of weeks entered 
        cout << "Enter hourly rate for the employee using this format (00.00): ";
        cin >> weeks[i].rate;
        cout << endl;        
    }
    return MAX_WEEKS;       //  All 4 weeks worked
}

//  Calculate regular pay for one week
double calculateRegPay (Week week)  //  Pass a specific week as argument
{   return week.hours * week.rate;    
}

//  Calculate OT pay for one week
double calculateOtPay(Week week)    //  Pass a specific week as argument
{   return (week.hours - 40) * (week.rate * 1.5) + (week.rate * 40);
}

//  Calculate gross pay
double calculateTotalPay (double regPay, double otPay)
{   return regPay + otPay;
}





I appreciate all of your help AbstractionAnon. When I run the program it is not calculating the gross properly.

Topic archived. No new replies allowed.