Need some help in coming up with a time-and-a-half equation

This is a discussion on Need some help in coming up with a time-and-a-half equation within the C++ Programming forums, part of the General Programming Boards category; Hi, I am creating a program that has tp determine the gross pay for each of several employees. And employee ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Need some help in coming up with a time-and-a-half equation

    Hi,

    I am creating a program that has tp determine the gross pay for each of several employees.
    And employee receives time-and-a-half for all hours worked in excess of 40 hours.

    The problem is, I just can't think of a way I can calculate this time-and-a-half. This is what iv'e come with so far -
    Code:
    int main()
    {
       double   hoursWorked;  // read in the hours worked by employee
       double   hourlyRate;   // read in the hourly rate of the employee
       double   salary;       // Print the gross pay of the employee
    
       cout << setprecision( 2 ) << fixed;
    
       while( hoursWorked != -1.0 ) {
       cout << "Enter hours worked ( -1 to end ): ";
       cin >> hoursWorked;
    
          if( hoursWorked != -1.0 ) {
          cout << "Enter hourly rate of the worker($00.00): ";
          cin >> hourlyRate;
    
          salary = hoursWorked * hourlyRate;
    
             if( hoursWorked < 40 )
                salary = hoursWorked
    Iv'e tried something like this - salary = (hoursWorked * hourlyRate) * 2 - but that wasn't correct.
    Can anyone please give me hand with this?

    Thank you

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Try something like:

    Code:
    salary = (hourlyRate * 1.5f) * hoursWorked;
    Time and a half, so you if you multiply the hourly rate by 3/2 or 1.5 you will get the adjusted hourly rate for overtime. If the time worked is over 40, calculate how much they made for the 40 hours then add to that how much they made for the over time. Hope this helps you.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Code:
    salary = (hoursWorked + (hoursWorked>40)?(hoursWorked-40)/2:0) * hourlyRate;
    //or 
    if(hoursWorked>40) hoursWorked += (hoursWorked-40)/2;
    salary = hoursWorked*hourlyRate;

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by grib
    Code:
    if(hoursWorked>40) hoursWorked += (hoursWorked-40)/2;
    salary = hoursWorked*hourlyRate;
    You should never make an unnecessary change an input just to make a calculation, as done here with hoursWorked

    Code:
    salary = hoursWorked*hourlyRate;
    if(hoursWorked>40) 
        salary += (hoursWorked - 40) * (hourlyRate / 2);
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    It almost runs correctly, except that, when I enter hours that are above standard hours, the program displays both the stand hours and over-time hours salary.
    I think I might not have set out these if/else structures correctly, but I can't seem to figure our what I need to do.
    Can anyone help me?

    Code:
    #include<iomanip>
    #include<conio>
    #include<iostream>
    using namespace std;
    
    int main()
    {
       double   workedHours;
       double   hourlyRate;
       double   normalSalary;
       double   overtimeSalary;
    
       double         extraHours = 0;
       const double   STANDARD_HOURS = 40.0;
    
       cout << setprecision( 2 ) << fixed;
    
       while ( workedHours != -1.0 )
       {
       cout << "Enter hours worked ( -1 to end ): ";
       cin >> workedHours;
    
          if ( workedHours != -1.0)
          {
             cout << "Enter hourly rate of the worker($00.00): ";
             cin >> hourlyRate;
    
             normalSalary = workedHours * hourlyRate;
    
             cout << normalSalary << endl << endl;
          }
    
             if ( workedHours > STANDARD_HOURS )
             {
                extraHours = workedHours - STANDARD_HOURS;
                workedHours = STANDARD_HOURS;
                overtimeSalary = (workedHours * hourlyRate) +
                (extraHours * 1.5 * hourlyRate);
    
                cout << "Salary is: " << overtimeSalary << endl << endl;
             }
       }
       getch();
       return 0;
    }
    Required output -


    Enter hours worked ( -1 to end ): 39
    Enter hourly rate of the worker ($00.00): 10.00
    Salary is: $390.00

    Enter hours worked( -1 to end ): 41
    Enter hourly rate of the worker ($00.00): 10.00
    Salary is: $415.00

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    There are several ways to do this. Here is one idea:
    Code:
          if ( workedHours != -1.0)
          {
             cout << "Enter hourly rate of the worker($00.00): ";
             cin >> hourlyRate;
    
             if ( workedHours > STANDARD_HOURS )
             {
                extraHours = workedHours - STANDARD_HOURS;
                workedHours = STANDARD_HOURS;
                overtimeSalary = (workedHours * hourlyRate) +
                (extraHours * 1.5 * hourlyRate);
    
                cout << "Salary is: " << overtimeSalary << endl << endl;
             }
             else
             {
                normalSalary = workedHours * hourlyRate;
    
                cout << "Salary is: " << normalSalary << endl << endl;
             }
          }
    
       }

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Originally posted by Guti14
    It almost runs correctly, except that, when I enter hours that are above standard hours, the program displays both the stand hours and over-time hours salary.
    I think I might not have set out these if/else structures correctly, but I can't seem to figure our what I need to do.
    Can anyone help me?
    I'm usually sympathetic to novices, and rightfully so, as even the simplest of problems can seem challenging when you're learning a language like C++. But you don't appear to be learning it. Far from it. I've seen your posts in this forum and elsewhere in other forums under a different name and it seems pretty obvious that you've written barely a line of code yourself. You simply take each problem that you've got, post it across different forums, take the corrected code, then post any new problems. You don't appear to have corrected any problems yourself at all.

    You can't possibly get anywhere useful with this approach. Sooner or later you're going to end up having to write something for yourself that is way, way beyond you - probably something that should be trivial to write.

    If you don't already have any, you need to get hold of some learning material, preferably a good beginner's book, some tutorials, a reference book, whatever. If you need some recommendations please ask and people here will be able to suggest some.

    Then you need to actually start thinking for yourself, and trying to solve your own problems, at least having a go, instead of expecting people to write your program for you incrementally.

    The number and nature of problems that you've exhibited in this and related threads demonstrate clearly that this program, for whatever reason you're writing it, is too difficult for you to write or even come close to writing. If you have any intention at all of being able to write C++ programs for yourself you need to put this program to one side and go back to learning the basics, the very basics. Basic syntax, iteration, selection, functions, C++ I/O. Until you acquire some skills with these basic C++ language building blocks you're pretty much wasting your time, and ours.

    Don't take offence. The above is spoken in good faith, and with the best of intentions. If you want to learn to write C++ programs, you're going to have to take a step back before you can start to go forward. As it is, all you're doing is getting other people to wrote your program for you.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21