Getting multiple values in a function

How come this returns a zero?
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

double calcPay (double hours);
int main()
{
cout << fixed << showpoint << setprecision(2);
double hours, rate;
cout << "Enter amout of hours worked: ";
cin >> hours;
cout << "Enter pay rate per hour: ";
cin >> rate;
double pay = calcPay(hours);

cout << " Your Gross pay plus over time is: $" << pay<< endl;
system("pause");
return 0;
}

double calcPay (double hours)
{
double pay, rate;


if (hours >40)

pay = hours * rate * 1.5;

else
pay = hours * rate;
return pay ;
}
closed account (D80DSL3A)
The 'rate' variable in the calcPay() function is different from the one in main().
You should also pass the pay rate to calcPay().
1
2
3
4
5
6
7
8
9
10
11
double calcPay (double hours, double rate)// also change the prototype above main
{
double pay;//, rate;

    if (hours >40)
        pay = hours * rate * 1.5;
    else
        pay = hours * rate;

    return pay ;
}

Call as double pay = calcPay(hours, rate);
in main.

Also, may I work 41 hours/week at your company? Getting paid OT for all 41 hours would be great!
thank you, after 4 hours trying to get it to work i came up with this:
Might not be the best way to do it but it worked. What would be the easiest way?
//Steven Calabrese
//Lab 4 Calculate gross pay
//Displays Gross Pay using a decision and 2 decimal places
//10/21/13
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

double calcPay (double hours, double reghours, double overhours, double rate);
int main()
{
cout << fixed << showpoint << setprecision(2);

double overhours, reghours, hours, rate;
cout << "Enter amout of hours worked: ";
cin >> hours;
cout << "Enter pay rate per hour: ";
cin >> rate;

if (hours > 40)
{
overhours = hours - 40;
reghours = hours - overhours;
}
else
{
overhours = hours;
}
double pay = calcPay(hours, reghours, overhours, rate);
cout << "Your Gross pay plus over time is: $" << pay<< endl;
system("pause");
return 0;
}

double calcPay (double hours, double reghours, double overhours, double rate)
{
double pay;


if (hours >40)

pay= overhours * rate * 1.5 + reghours * rate;
//pay = (hours - 40) * (rate * .5 )+ (hours * rate);

else
pay = overhours * rate;
return pay;
}
closed account (D80DSL3A)
You could make the variables reghours and overhours local to calcPay() rather than main() and do the calcs. there instead. Then you are passing only hours and rate to the calcPay().
Example? Does that mean put all my calcs into int main?

closed account (D80DSL3A)
No. I meant to do all the calcs in calcPay. Sorry if I worded that poorly.
1
2
3
4
5
6
7
8
double calcPay (double hours, double rate)
{
    double pay=0.0, overhours=0.0, reghours=0.0;// or other suitable initial values?

    // all calcs here
   
    return pay;
}

EDIT: Although your approach with the overhours and reghours variables word work I think it's more complicated than necessary. I would:
1) initialize pay as though all hours are paid at rate.
2) add the OT premium to pay for hours over 40.
This gives 3 lines of code in calcPay, with no reghours or overhours variables involved.
Last edited on
Topic archived. No new replies allowed.