By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

turing into a function

P: 10
I'm trying to turn this part into a function into my code

Expand|Select|Wrap|Line Numbers
  1.  
  2. grosspay = (hours * rate);
  3.       if (hours > 40)
  4.       grosspay = ((hours - 40) * rate * 1.5) + (40 * rate);
  5.       if (hours > 60)
  6.       grosspay = ((hours - 60) * rate * 2) + (20 * rate * 1.5) +(40 * rate);
  7.  
  8.  


does anyone have a sugestion? whenever I do it I get errors. please help



heres the code

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4.  
  5. /* Function Prototype */
  6.  
  7. /* Main Program */
  8.  
  9. int main(void)
  10. {
  11.  int empNum, sumh, sump;
  12.    double hours, rate, grosspay;
  13.  
  14.    cout << setprecision(2) << setiosflags(ios::fixed);
  15.  
  16.    cout << "Please enter the employee number (0 to stop): ";
  17.    cin >> empNum;
  18.  
  19.    while (empNum != 0)
  20.    {
  21.     cout << "Hours: ";
  22.       cin >> hours;
  23.       cout << "Rate: ";
  24.       cin >> rate;
  25.  
  26.       grosspay = (hours * rate);
  27.       if (hours > 40)
  28.       grosspay = ((hours - 40) * rate * 1.5) + (40 * rate);
  29.       if (hours > 60)
  30.       grosspay = ((hours - 60) * rate * 2) + (20 * rate * 1.5) +(40 * rate);
  31.  
  32.  
  33.       cout << "\nGross Pay is " << grosspay;
  34.  
  35.       cout << "\n\nPlease enter the employee number (0 to stop): ";
  36.     cin >> empNum;
  37.    }
  38.  
  39.              cout << "Please enter the first employee's hours: ";
  40.              cin >> hours;
  41.  
  42.              cout << "Please enter the second employee's hours: ";
  43.              cin >> hours;
  44.  
  45.              cout << "Please enter the third employee's hours: ";
  46.              cin >> hours;
  47.  
  48.              cout << "Please enter the fourth employee's hours: ";
  49.              cin >> hours;
  50.  
  51.              cout << "Please enter the fifth employee's hours: ";
  52.              cin >> hours;
  53.  
  54.              cout << "Please enter the sixth employee's hours: ";
  55.              cin >> hours;
  56.  
  57.              sumh = hours + hours + hours + hours + hours + hours;
  58.  
  59.    cout << "\nThe total hours worked by all employees is " << sumh << endl;
  60.  
  61.  
  62.              cout << "Please enter the first employee's grosspay: ";
  63.              cin >> grosspay;
  64.  
  65.              cout << "Please enter the second employee's grosspay: ";
  66.              cin >> grosspay;
  67.  
  68.              cout << "Please enter the third employee's grosspay: ";
  69.              cin >> grosspay;
  70.  
  71.              cout << "Please enter the fourth employee's grosspay: ";
  72.              cin >> grosspay;
  73.  
  74.              cout << "Please enter the fifth employee's grosspay: ";
  75.              cin >> grosspay;
  76.  
  77.              cout << "Please enter the sixth employee's grosspay: ";
  78.              cin >> grosspay;
  79.  
  80.    sump = grosspay + grosspay + grosspay + grosspay + grosspay + grosspay;
  81.  
  82.    cout << "\nThe total gross pay earned by employees is " << sump << endl;
  83.  
  84.    return (0);
  85. }
  86.  
  87. /* Function: GrossPay
  88.  * Usage: grosspay = GrossPay(hours, rate);
  89.  * ----------------------------------------
  90.  * This function calculates the gross pay for an employee for the
  91.  * given number of hours worked (hours) and the rate of pay (rate).
  92.  * An employee is paid at the regular rate for the first 40 hours worked,
  93.  * at 1.5 times the rate for any hours over the first 40, and
  94.  * at 2 times the rate for any hours over the first 60.
  95.  *
  96.  *   e.g.  62 hours at $10 per hour,
  97.  *         gives $400 for the first 40 hours,
  98.  *           $300 for the next  20 hours, and
  99.  *         $ 40 for the final  2 hours,
  100.  *                   ----               --
  101.  *       thus  $740 for the total 62 hours for a gross pay.
  102.  */
  103.  
Nov 1 '06 #1
Share this Question
Share on Google+
6 Replies


Banfa
Expert Mod 5K+
P: 8,916
does anyone have a sugestion? whenever I do it I get errors. please help
Well then post what you have tried and the errors you are getting.
Nov 1 '06 #2

P: 10
heres what i have but for hours over 60 the output is wrong but it should be right acording to the code..can you help?

#include <iostream>
#include <iomanip>

/* Function Prototype */
double grosspaycalc(int hours, int rate);

/* Main Program */

int main(void)
{
int empNum, sumh, sump;
double hours, rate, grosspay;


cout << setprecision(2) << setiosflags(ios::fixed);

cout << "Please enter the employee number (0 to stop): ";
cin >> empNum;

while (empNum != 0)
{
cout << "Hours: ";
cin >> hours;
cout << "Rate: ";
cin >> rate;

grosspay = grosspaycalc(hours, rate);



cout << "\nGross Pay is " << grosspay;

cout << "\n\nPlease enter the employee number (0 to stop): ";
cin >> empNum;
}

cout << "Please enter the first employee's hours: ";
cin >> hours;

cout << "Please enter the second employee's hours: ";
cin >> hours;

cout << "Please enter the third employee's hours: ";
cin >> hours;

cout << "Please enter the fourth employee's hours: ";
cin >> hours;

cout << "Please enter the fifth employee's hours: ";
cin >> hours;

cout << "Please enter the sixth employee's hours: ";
cin >> hours;

sumh = hours + hours + hours + hours + hours + hours;

cout << "\nThe total hours worked by all employees is " << sumh << endl;


cout << "Please enter the first employee's grosspay: ";
cin >> grosspay;

cout << "Please enter the second employee's grosspay: ";
cin >> grosspay;

cout << "Please enter the third employee's grosspay: ";
cin >> grosspay;

cout << "Please enter the fourth employee's grosspay: ";
cin >> grosspay;

cout << "Please enter the fifth employee's grosspay: ";
cin >> grosspay;

cout << "Please enter the sixth employee's grosspay: ";
cin >> grosspay;

sump = grosspay + grosspay + grosspay + grosspay + grosspay + grosspay;

cout << "\nThe total gross pay earned by employees is " << sump << endl;

return (0);
}

/* Function: GrossPay
* Usage: double grosspaycalc(int hours, int rate);
* ----------------------------------------
* This function calculates the gross pay for an employee for the
* given number of hours worked (hours) and the rate of pay (rate).
* An employee is paid at the regular rate for the first 40 hours worked,
* at 1.5 times the rate for any hours over the first 40, and
* at 2 times the rate for any hours over the first 60.
*
* e.g. 62 hours at $10 per hour,
* gives $400 for the first 40 hours,
* $300 for the next 20 hours, and
* $ 40 for the final 2 hours,
* ---- --
* thus $740 for the total 62 hours for a gross pay.
*/

double grosspaycalc(int hours, int rate)

{
if (hours <= 40)
return (hours * rate);
if (hours > 40)
return ((hours - 40) * rate * 1.5) + (40 * rate);
if (hours > 60)
return ((hours - 60) * rate * 2) + (20.0 * rate * 1.5) +(40 * rate);

return 0.0;
}
Nov 2 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
heres what i have but for hours over 60 the output is wrong but it should be right acording to the code..can you help?}
Well it looks right, give me an example of input and output where it goes wrong
Nov 2 '06 #4

P: 10
Well it looks right, give me an example of input and output where it goes wrong
62 hours and 10 rate should be 740 but output says 730...
Nov 2 '06 #5

P: 10
I got it guys, Thanks again
Nov 2 '06 #6

Banfa
Expert Mod 5K+
P: 8,916
I got it guys, Thanks again
Tell us what it was please :D
Nov 2 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.