Thread: How do I get these calculations correct?

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    8

    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";
     
    }

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    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.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "Please help me figure out why my calculations are incorrect"

    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?
    Last edited by 7stud; 04-06-2003 at 07:41 PM.

  4. #4
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    What exactly isn't calculating correctly?
    If you narrow down the answer than it's easier to fix it.
    If you ever need a hug, just ask.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    8

    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.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Code:
    for(int i=0; i<7; i++)
    {
    cout << "Hours worked on " << day[i] <<": ";
    cin >> TotalHours;
    TotalHours += TotalHours;
    }
    Your problem may be here.

    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.

  7. #7
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    I was just about to say that...
    If you ever need a hug, just ask.

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    8
    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.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Your welcome.

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    7stud may have been a bit insensitive, but his/her methods and recommendations are an excellent way to improve your ability to successfully debug your own code. I encourage you to consider the approach recommended by 7stud.

  11. #11
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    Originally posted by elad
    7stud may have been a bit insensitive, but his/her methods and recommendations are an excellent way to improve your ability to successfully debug your own code. I encourage you to consider the approach recommended by 7stud.
    I second that. Being a good debugger is part of being a good programmer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux for GNU/Linux is not correct?
    By password636 in forum Linux Programming
    Replies: 8
    Last Post: 03-31-2009, 08:30 PM
  2. Doing calculations on a data file
    By bassist11 in forum C Programming
    Replies: 2
    Last Post: 03-30-2009, 07:47 PM
  3. Is this correct : passing strings?
    By socket in forum C Programming
    Replies: 15
    Last Post: 11-25-2008, 02:03 PM
  4. correct malloc use
    By s_siouris in forum C Programming
    Replies: 10
    Last Post: 05-28-2008, 10:49 PM
  5. C program compile but not doing the calculations
    By abs.emailverify in forum C Programming
    Replies: 8
    Last Post: 11-08-2006, 08:43 AM