How do I get these calculations correct?

• 04-06-2003
How do I get these calculations correct?
Could someone Please help me figure out why my calculations are incorrect. I have been working very hard on this and am really confused. I do not see what I am doing wrong or how to fix it. I do not get any error messages. However, when I run the program, the calculations are all incorrect. Please help me out with this.

Code:

```#include <iostream> #include <iomanip> using namespace std;   double Gross(double); char mStatusDetermination(); double FederalTax(char, double); double StateTax(double); double LocalTax(double); double MedInsuranceDeductions(char); double TakeHomePay(double, double, double); void DisplayPayrollReport(double, double, double,double, double, double, double);   int main(){         char ans;         char MaritalStatus;         double GrossPay, NetPay;     double HourlyRate;         double federalDeductions, stateDeductions, localDeductions;         double TotalDeductions, ExtraDeductions;                                 cout << "                    Welcome To The Payroll Calculation Program. \n";                 cout << "--------------------------------------------------------------------------------\n";                 cout << endl;         do         {         cout << "The rate per hour is: ";     cin >> HourlyRate;         cout << endl;     GrossPay = Gross(HourlyRate);     MaritalStatus = mStatusDetermination();     federalDeductions = FederalTax(MaritalStatus, GrossPay);     stateDeductions = StateTax(GrossPay);     localDeductions = LocalTax(GrossPay);     TotalDeductions = (federalDeductions + stateDeductions + localDeductions);     ExtraDeductions = MedInsuranceDeductions(MaritalStatus);     NetPay = TakeHomePay(GrossPay,TotalDeductions, ExtraDeductions);     DisplayPayrollReport(GrossPay, HourlyRate, federalDeductions, stateDeductions, localDeductions, ExtraDeductions, NetPay);       cout << "\nWould you like to calculate another?(Y/N)? ";     cin >> ans;     cout << endl;         }         while(ans == 'Y' || ans == 'y');           char letter;         cout<<"Enter a letter to end the program:\n";         cin>>letter;             return 0; }   /************************************************************************************************************************/ double Gross(double HourlyRate) {         double TotalHours = 0.0;         double HoursWorked;         double OvertimeHrs = 0.0;         double RegPay;         double OvertimePay, grossPay;     char *day[7]={"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};             for(int i=0; i<7; i++)         {         cout << "Hours worked on " << day[i] <<": ";         cin >> HoursWorked;         HoursWorked += HoursWorked;         }           cout << endl;         if(HoursWorked < 40)         {         RegPay = HourlyRate * HoursWorked;         }         TotalHours = TotalHours + HoursWorked;         if(TotalHours > 40)         {         OvertimePay = OvertimeHrs * RegPay * 1.5;         grossPay = OvertimePay * RegPay;         }                return (RegPay + OvertimePay); }   /************************************************************************************************************************/ char mStatusDetermination() {           char mStatus;         int count = 0;           do         {     cout << "Is Marital Status Married or Single?\n";     cout << "(M=married/S=single):\t";     cin >> mStatus;     cout << endl << endl;       if(mStatus == 'M' || mStatus == 'm' || mStatus == 'S' || mStatus == 's')                 count++;         }         while(count < 1);         return mStatus;   } /************************************************************************************************************************/ double FederalTax(char MaritalStatus, double GrossPay) {     const double MarriedFedTax =.15;         const double SingleFedTax =.20;           if(MaritalStatus == 'M' || MaritalStatus == 'm')         {                 return (GrossPay * MarriedFedTax);         }         else         {                 return (GrossPay * SingleFedTax);     }   } /************************************************************************************************************************/ double StateTax(double GrossPay) {   const double StateTaxRate = .04;     return (GrossPay * StateTaxRate);   } /************************************************************************************************************************/ double LocalTax(double GrossPay) {         const double LocalTaxRate = .02;           return (GrossPay * LocalTaxRate);   } /************************************************************************************************************************/ double MedInsuranceDeductions(char MaritalStatus) {         const double MarriedDeductionRate = 18.00;         const double SingleDeductionRate = 10.00;             if(MaritalStatus == 'M' || MaritalStatus == 'm')         {                 return MarriedDeductionRate;         }         else         {                 return SingleDeductionRate;         } } /************************************************************************************************************************/ double TakeHomePay(double GrossPay, double TotalDeductions, double ExtraDeductions) {         return (GrossPay - TotalDeductions - ExtraDeductions); } /************************************************************************************************************************/ void DisplayPayrollReport(double GrossPay, double HourlyRate, double federalDeductions, double stateDeductions,                                               double localDeductions, double ExtraDeductions, double NetPay) {                 cout<<"  Employee Earnings are:\n"                 <<"----------------------------------------\n"         <<setw(15)<<"Gross Pay"<<setw(9)<<setprecision(2)<<setiosflags(ios::fixed | ios::showpoint)<<GrossPay<<"\n"                 <<setw(15)<<"Pay Rate"<<setw(9)<<HourlyRate<<"\n\n\n\n"                 <<"  Deductions for the WorkWeek are:\n"                 <<"------------------------------------------\n"                 <<setw(15)<<"Federal Taxes"<<setw(9)<<federalDeductions<<"\n"         <<setw(15)<<"State Taxes"<<setw(9)<<stateDeductions<<"\n"         <<setw(15)<<"Local Taxes"<<setw(9)<<localDeductions<<"\n"         <<setw(15)<<"Insurance"<<setw(9)<<ExtraDeductions<<"\n\n\n\n"                 <<setw(15)<<"Net Pay"<<setw(9)<<NetPay<<"\n";   }```
• 04-06-2003
gamer4life687
Im not 100% sure even though im doing this in school now but this isnt correct

Code:

`grossPay = OvertimePay * RegPay;`
you could be looking for this

Code:

`grossPay = OvertimePay + RegPay;`
But this would be net pay

Code:

`netpay = OvertimePay + RegPay;`
Remember im not 100% sure so maybe some will correct me.
Hope it helps.
• 04-06-2003
7stud

You start over. Then take your first function, and test it in main() so that it can handle all the cases that may occur. Then, you add your second function, and test it in main(), and so on. After you've tested all your functions, then you code your main() function, and run it. If there are bugs, you track them down to a few lines of code and figure it out. If you can't, try taking some similar lines and create a separate program, and test it in isolation until you figure out the problem. You may not understand what's wrong, but at least you will have identified where things are going wrong.

Then, if you still can't figure out how to correct the problem, you should post some code that demonstrates the problem--it shouldn't be code from your program, rather it should be some code that isolates and demonstrates the problem and uses variable names that are easily understood and descriptive. Then hopefully someone will be able to tell you what's wrong. Posting your whole program, and asking, "what's wrong" is pretty irritating, not to mention completely lazy. Didn't you read "how to post a question" when you entered the bulletin board?
• 04-06-2003
CheesyMoo
What exactly isn't calculating correctly?
If you narrow down the answer than it's easier to fix it.
• 04-06-2003
Where are my error that's causing incorrect output?
7Stud, Are you here to insult people? My intentions in coming to this message board are not to be attacked, nor is it to be irritating to you or anyone else. I am far from Lazy. I am new to programming, period. Don't discourage people from attempting to get help by trying to be insulting. If you feel that someone is irritating with their post then don't reply, better yet, don't read it! Bottom Line!

Thank you CheesyMoo and gamer4life687 for your tactful responses!

Anyways, to anyone out there who is willing to help me and not try to attack me for trying to get help from others who are more knowledgeable in this field, your feedback will be appreciated.

I have worked on my code and made some changes to try to get my code to work properly. I have been unable to get my program to caculate the employees total pay including their overtime pay. It seems during runtime that my program will calculate the RegularPay but anything over the 40 hours is not calculated properly. Here is the portion of my code where I think the problem is but cant seem to figure it out.

Code:

```double Gross(double HourlyPayRate) {         double TotalHours;         double RegHours;         const int FULL_TIME_HOURS = 40;         double OvertimeHrs = 0.0;         double RegPay;         double OvertimePay;     char *day[7]={"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};             for(int i=0; i<7; i++)         {         cout << "Hours worked on " << day[i] <<": ";         cin >> TotalHours;         TotalHours += TotalHours;         }           cout << endl;         if(TotalHours > FULL_TIME_HOURS)         {                 OvertimeHrs = TotalHours - FULL_TIME_HOURS;         }         else         {                 RegHours = FULL_TIME_HOURS;         }                         RegPay = RegHours * HourlyPayRate;                 OvertimePay = OvertimeHrs * RegPay * 1.5;                 return (RegPay + OvertimePay); }```
I look forward to hearing from those who are really on this board to help.
• 04-06-2003
alpha
Code:

```for(int i=0; i<7; i++) { cout << "Hours worked on " << day[i] <<": "; cin >> TotalHours; TotalHours += TotalHours; }```

You are inputting into total hours. and then trying to add to it. this defeats the purpose because when you input, you are wiping out what used to be there.

instead, have another variable, and then input into that, and then add it to Total hours. i.e.
Code:

```double hours; // rest of variables // ... for(int i=0; i<7; i++) { cout << "Hours worked on " << day[i] <<": "; cin >> hours; TotalHours += hours; }```
try that.
• 04-06-2003
CheesyMoo
I was just about to say that...
• 04-06-2003
I have given that a try and my output is still incorrect. I am going to try a few more things and see what happens before I post again. Thanks a bunch for your help.
• 04-07-2003
7stud