I am attempting to calculate the hours and rate for the week for four weeks and then calculate the gross pay of all four weeks for a monthly total. I am not able to get the program to calculate the monthly. Any help would be great.
#include <iostream>
#include <cstdlib>
#include <cmath>
usingnamespace std;
double calcWeekPay (double rate, double hours)
{
//In this function return the pay for the week, not months gross (check for overtime)
}
int main()
{
constdouble OVERTIME_RATE = 1.5;
// declare variables
double hours = 0;
double rate = 0;
double gross = 0; //Don't forget to initialize this variable or you might end up with garbage values.
double weekPay = 0; //add a variable for the current weeks pay
int weekBeingPaid = 1;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision (2);
cout << "Enter employee's hourly rate: ";
cin >> rate;
//while loop to get the weekly amounts
while (weekBeingPaid <=4)
{
cout << "Enter hours worked for week " << weekBeingPaid<< ": ";
cin >> hours;
// calculate pay
//Here run your calcWeekPay function and return its value to weekPay
//add the week pay to your months gross
cout <<"Employee's pay for the week "<<weekBeingPaid<<" is: "<< weekPay <<endl;
weekBeingPaid++;
}
cout << "Employee's total gross pay for the month is: "<< gross <<endl;
// Display results
return 0;
}
//I included the variable weekly and changed the gross to monthly to make more sense. I attempted to put weekly (weekPay) to add the week pay to the month gross but calculations were off. So my overtime is not calculating and monthly gross is not adding up. Still confused on what I am doing wrong. Thank you for your helpl.
Line 62 and line 73, both have the same type of error. You can't have 2 equal signs in the same line. Change the second equals to a +, like you have on lines 41 and 81.
line 90 should be monthly variable . Because of the loop the amount monthly doubles on each iteration , So a quick and dirty fix is to divide by 2 monthly/2.
It seems like your headed in the right direction (although there may be some unnecessary calculations/variables in there, but we can handle that later).
Take some time and check every case, what if you work only 30 hours every week, what if you work 40 hours every week, what if you work 45 hours every week. Figure out what your answer for the weekly calculations should be and see if that is correct. Figure out what your answer for the total months calculation should be and check if that is correct. Testing each of these cases will help you narrow down the case where your calculations are not doing what you think they are doing.
Simplify as much as possible, its a lot easier to make mistakes when you make the same calculation in various places instead of just one place. HINT: Don't update the monthly pay in each of the if/else if blocks, wait until you have calculated the weekly pay in those blocks then after just set monthly to monthly + the weekly pay just calculated.