Function Question

Write a C++ program to compute a person's weekly wage. The main function should ask the user to enter the person's pay rate and the number of hours worked.
The program should display the person's gross pay, income tax, and net pay.

Use a function Gross_Pay() to calculate the gross pay. Pass two arguments to the function, the pay rate and the hours worked. The function should return the gross pay, including overtime to main(). Overtime is paid at 1.5 times the regular rate for hours worked over 40.

Use a function Tax() to calculate the person's tax. Pass one argument to the function, the person's gross pay. Tax() should calculate the tax as 15% of the gross pay. The function should return the tax to main().

Use a function Net_Pay() to calculate the net pay. Pass two parameters to the function, the gross pay and the tax. The function should return the net pay to main(). The net pay is the gross pay minus the tax.


( Use a function Display_Results() to display the gross pay, tax, and net pay, which you should pass to the function as arguments. Display_Results() should not return a value ) In this statement here, how do I display the results with to pass function as arguments.

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
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
double Gross_Pay(double&, double&, double&, double, double);
double Tax(double&, double&);
double Net_Pay(double&, double&, double&);
double display_results(); //haven't used in my code : HELP *

double Gross_Pay(double& regularpay, double& overtimepay, double& grosspay, double payrate, double hoursworked)
{
	regularpay = hoursworked * payrate;
	overtimepay = (hoursworked-40) * payrate * 1.5;
	grosspay = regularpay + overtimepay;

	return grosspay; 
}

double Tax(double& tax, double& grosspay)
{
	tax = grosspay * 0.15;

	return tax;
}

double Net_Pay(double& netpay, double& grosspay, double& tax)
{
	netpay = grosspay - tax ;

	return netpay;
}

int main()
{
	double regularpay,overtimepay,payrate,hoursworked,grosspay,tax,netpay;

	cout<<"Enter number of hours worked:"; cin>>payrate;
	cout<<"Enter pay rate:"; cin>>hoursworked;

	Gross_Pay(regularpay,overtimepay,grosspay,payrate,hoursworked);
	Tax(tax,grosspay);
	Net_Pay(netpay,grosspay,tax);

	cout<<left<<setw(20)<<"GROSS PAY"<<setw(20)<<"TAX"<<setw(20)<<"NET PAY"<<endl;
	cout<<left<<setw(20)<<grosspay<<setw(20)<<tax<<setw(20)<<netpay<<endl;

	system("Pause");
	return 0;
}


Appreciate if anyone would help me to point out my mistake and help me to improve my efficiency of this program. Thank you. :)
Last edited on
What is the question?

Pass values by value rather than by reference.

double Net_Pay(double& netpay, double& grosspay, double& tax)
double Net_Pay(double netpay, double grosspay, double tax)
-----------------------------------------------
C++ Homework Help starting from $10
http://www.assignments.me/cpp.html
Last edited on
I agree with codehelper however be sure to assign the return value to a variable in main.
Or you can just print directly

 
cout << left << setw(20) << grosspay << setw(20) <<tax << setw(20) << Net_Pay(netpay,grosspay,tax) << endl;
closed account (48T7M4Gy)
Still need to use <iomanip> functionality to show dollars and 2 figure dollar precision. If you don't like return directly from an expression then create a local variable in the function and return that.

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

using namespace std;

double Gross_Pay(double, double);
double Tax(double, double);
double Net_Pay(double, double);
double display_results(); //haven't used in my code : HELP *

double Gross_Pay(double payrate, double hoursworked)
{
    if( hoursworked > 40 )
        return (hoursworked - 40) * payrate * 1.15 + 40 * payrate;
    else
        return hoursworked * payrate;
}

double Tax( double grosspay )
{
    return grosspay * 0.15;
}

double Net_Pay(double gross_pay, double tax_amount)
{
    return gross_pay - tax_amount ;
}

int main()
{
    double payrate = 0;
    double hoursworked = 0;
    double grosspay = 0;
    double tax = 0;
    double netpay = 0;
    
    cout<<"Enter pay rate: ";
    cin>>payrate;
    
    cout<<"Enter hours worked: ";
    cin>>hoursworked;
    
    grosspay = Gross_Pay(payrate, hoursworked);
    tax = Tax(grosspay);
    netpay = Net_Pay(grosspay, tax);
    
    cout<<left<<setw(20)<<"GROSS PAY"<<setw(20)<<"TAX"<<setw(20)<<"NET PAY"<<endl;
    cout<<left<<setw(20)<<grosspay<<setw(20)<<tax<<setw(20)<<netpay<<endl;
    
    return 0;
}
Still need to use <iomanip> functionality to show dollars and 2 figure dollar precision.

If you're not using Windows (they don't support localization), you can do it the standard C++ way, with std::put_money

http://en.cppreference.com/w/cpp/io/manip/put_money
http://www.cplusplus.com/reference/iomanip/put_money/

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <iomanip>

int main()
{
    double grosspay = 12345.56789; // note: number in Cents for US locale
    std::cout.imbue(std::locale("en_US"));
    std::cout << std::showbase << std::put_money(grosspay) << '\n';
}
$123.46


live demo http://melpon.org/wandbox/permlink/DZLg1zl0MAsjjeNA

(on Windows, you'll have to do it by hand, with manipulators such as std::setprecision and std::fixed)
Last edited on
closed account (48T7M4Gy)
Even better.

( I was thinking setprecision etc with a $ sign independently added to the stream knowing that roundoff errors need to be accounted for, seeing that it is dreaded money. )
Thanks for the reply. Btw I'm using windows. Is it okay I stick with setprecision and fixed ? because I'm just a beginner new to C++.. the key for put_money I haven't learned yet. :D
closed account (48T7M4Gy)
Use whichever one you want. If you understand the put_money option by reading the reference and properly use it then go for it. Another possibility is to use both ways and blind your teacher with science :)
just use
<<"$"<<fixed<<setprecision(2)<<amount
avoid put_moneys
closed account (48T7M4Gy)
avoid put_moneys

?
@kemort @codehelper okay Thankyou :) I got it now .
2kemort
most likely this is a homework assignment. It's based on some course or book.
fixed<<setprecision(2) is a standard way to do such things.
Also it's work under any platform.
Most likely you are using international version of Windows. So you are forced to use
std::cout.imbue(std::locale("en_US"));
for example google "std::cout.imbue(std::locale("
just few pages. This is not a comon way.
closed account (48T7M4Gy)
Excellent points you raise @codehelper and I now have a clearer picture of where you are coming from. Like you, I'd be surprised if it wasn't homework too!

To tell you the truth, and why wouldn't I, I use Xcode on a MBPro OSX. I tossed in Windows about 2 years ago. I haven't tried Cubbi's suggestion so I can't comment on compatibility with absolute certainty but I have a nagging suspicion it will be OK on my machine.

Cheers and best wishes for the New Year :)
Welcome. Same to you and all readers :)
closed account (48T7M4Gy)
FWIW Cubbi's suggestion works and if you change the local to "en_GB" we get pounds.
Topic archived. No new replies allowed.