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

• 01-12-2004
Guti14
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
• 01-12-2004
MrWizard
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.
• 01-12-2004
grib
Code:

```salary = (hoursWorked + (hoursWorked>40)?(hoursWorked-40)/2:0) * hourlyRate; //or if(hoursWorked>40) hoursWorked += (hoursWorked-40)/2; salary = hoursWorked*hourlyRate;```
• 01-12-2004
WaltP
Quote:

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);```
• 01-13-2004
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?

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
• 01-13-2004
swoopy
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;         }       }   }```
• 01-14-2004
Omnius
Quote:

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.