Thread: my answer correct or not??

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    10

    Unhappy my answer correct or not??

    hello every body

    this the question

    An employee is paid at rate of 16.78$ per hour for the first 40 hours worked in a week. Any hours over that are paid at the overtime rate of one and one half times that. From the worker's gross pay, 6% is witheld for social security tax, 14% is witheld for federal income tax, 5% is witheld for state income tax, and 10 dollars per week is witheld for union dues. If the worker has three or more dependents, then the additional 35 dollars is withheld to cover the extra cost of helf insurance beyond what the employer pays. Write the program that will read in the number of hours worked in a week and the number of dependents as input, and will then output the worker's gross pay , each witholding amount , and the net take home pay for the week. For the harder version , write your program so that it allows the calculation to be repeated as often as the user wishes.

    and this my answer

    but i don't know if true or false

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	const double rate=16.78;
        const double social_tax= 0.06;
        const double federal_tax=0.14;
        const double state_tax=0.05;
        int hours,dependents; 
        double net_pay,gross_pay,overtime,state_tax_pay,pay_overtime,social_tax_pay,federal_tax_pay;
        cout<<"how many hours you worked this week: (0 to exit) ";
        cin>>hours;
    	while (hours>0)
    	{
    		cout<<"How many children you have?: ";
            cin>>dependents;
    		if(hours>40)
    		{
    			if (dependents>=3)
    			{ 
    				overtime=hours-40;
    				pay_overtime=overtime*(rate*1.5);
    				gross_pay= (rate*hours)+pay_overtime;
    				cout<<"Your gross pay is: "<<gross_pay<<endl;
    				social_tax_pay=gross_pay*social_tax;
    				cout<<"Your sosial tax pay is equal to: "<<social_tax_pay<<endl;
    				federal_tax_pay=gross_pay*federal_tax;
    				cout<<"Your federal tax pay is equal to: "<<federal_tax_pay<<endl;
    				state_tax_pay=gross_pay*state_tax;
    				cout<<"Your state tax pay is equal to: "<<state_tax_pay<<endl;
    				cout<<"You have to pay additional 35$ becouse you heve more then two dependends\n";
    				net_pay=gross_pay-social_tax_pay-federal_tax_pay-state_tax_pay-35;
    				cout<<"Your net pay is equeall to: "<<net_pay;
    			}
    			else
    			{	
    				overtime=hours-40;
    	            pay_overtime=(rate*1.5)*overtime;
    	            gross_pay= (rate*hours)+pay_overtime;
    	            cout<<"Your gross pay is: "<<gross_pay<<endl;
    	            social_tax_pay=gross_pay*social_tax;
    	            cout<<"Your sosial tax pay is equal to: "<<social_tax_pay<<endl;
    	            federal_tax_pay=gross_pay*federal_tax;
    	           cout<<"Your federal tax pay is equal to: "<<federal_tax_pay<<endl;
    	           state_tax_pay=gross_pay*state_tax;
    	           cout<<"Your state tax pay is equal to: "<<state_tax_pay<<endl;
    	           net_pay=gross_pay-social_tax_pay-federal_tax_pay-state_tax_pay;
    	           cout<<"Your net pay is equeall to: "<<net_pay;
    
    }
    }
    else 
    { 
    if (dependents>=3)
    {
    	gross_pay= rate*hours;
    	cout<<"Your gross pay is: "<<gross_pay<<endl;
    	social_tax_pay=gross_pay*social_tax;
    	cout<<"Your sosial tax pay is equal to: "<<social_tax_pay<<endl;
    	federal_tax_pay=gross_pay*federal_tax;
    	cout<<"Your federal tax pay is equal to: "<<federal_tax_pay<<endl;
    	state_tax_pay=gross_pay*state_tax;
    	cout<<"Your state tax pay is equal to: "<<state_tax_pay<<endl;
    	cout<<"You have to pay additional 35$ becouse you heve more then two dependends\n";
    	net_pay=gross_pay-social_tax_pay-federal_tax_pay-state_tax_pay-35;
    	cout<<"Your net pay is equeall to: "<<net_pay;
    }
    else
    {
    	gross_pay= rate*hours;
    	cout<<"Your gross pay is: "<<gross_pay<<endl;
    	social_tax_pay=gross_pay*social_tax;
    	cout<<"Your sosial tax pay is equal to: "<<social_tax_pay<<endl;
    	federal_tax_pay=gross_pay*federal_tax;
    	cout<<"Your federal tax pay is equal to: "<<federal_tax_pay<<endl;
    	state_tax_pay=gross_pay*state_tax;
    	cout<<"Your state tax pay is equal to: "<<state_tax_pay<<endl;
    	net_pay=gross_pay-social_tax_pay-federal_tax_pay-state_tax_pay;
    	cout<<"Your net pay is equeall to: "<<net_pay;
    }
    }
    
    cout<<endl;
    cout<<"how many hours you worked this week: (0 to exit)";
    cin>>hours;
    
    }
    return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    True or false ? This is not about being true or false but rather being correct or not. This is not even about being correct or not, but rather about producing the expected results or not. Does it output the expected results ? Then your code is fine.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    10
    Quote Originally Posted by Desolation View Post
    True or false ? This is not about being true or false but rather being correct or not. This is not even about being correct or not, but rather about producing the expected results or not. Does it output the expected results ? Then your code is fine.
    thnx bro 4 your comment

    and sorry about my poor english.

  4. #4
    Registered User magicxsoldier's Avatar
    Join Date
    Feb 2006
    Location
    Houston Texas
    Posts
    4
    Your code is fine, I have tried it out, the only problems I see are some spelling issues.

    Sosial should be Social
    Equall should be Equal
    becouse should be because
    Dependends should be Dependents


    That is all, good luck..

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    This calculation is wrong:
    Code:
    overtime=hours-40;
    pay_overtime=overtime*(rate*1.5);
    gross_pay= (rate*hours)+pay_overtime;
    When you've worked overtime, the amount of hours over 40 gets the time-and-a-half pay rate (that's done correctly). The other 40 hours worked gets the normal pay rate but you haven't adjusted hours to take that into account. You're calculating based on the original (over 40) hours worked and you'll end up paying the worker more than what is deserved. You need to either cap hours at 40 or just use a hard-coded 40 value there in place of hours, i.e.:
    Code:
    gross_pay= (rate*40)+pay_overtime;

    Other than that, there is a lot of duplicated code there that can be combined.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Code:
    overtime=hours-40;
    pay_overtime=overtime*(rate*1.5);
    gross_pay= (rate*hours)+pay_overtime;
    What if you didn't work overtime? You'll end up with a negative value here

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by h_howee
    What if you didn't work overtime? You'll end up with a negative value here
    That's part of an if block that takes care of that:
    Code:
    if(hours>40)
    {
        if (dependents>=3)
        {
            overtime=hours-40;
            pay_overtime=overtime*(rate*1.5);
            gross_pay= (rate*hours)+pay_overtime;
            ...
        }
        else
        {
            overtime=hours-40;
            pay_overtime=overtime*(rate*1.5);
            gross_pay= (rate*hours)+pay_overtime;
            ...
        }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Keep getting negative sign after answer
    By uptown11 in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2009, 11:14 PM
  2. how would you answer this question?
    By smoking81 in forum Linux Programming
    Replies: 3
    Last Post: 09-08-2008, 03:53 AM
  3. Getting the same answer, when the program restarts
    By Tippie in forum C# Programming
    Replies: 1
    Last Post: 05-31-2008, 01:45 PM
  4. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  5. getting weird answer when not using ".h"
    By CobraCC in forum C++ Programming
    Replies: 10
    Last Post: 05-07-2003, 06:21 AM