# If statement

how do I do an if statement when > 80 hours = .5 ( time and a half ) else < 80 = 0
LVL 1
###### Who is Participating?

Commented:
change this:
if (hoursWorked > 80)
grossPay=((hoursWorked-80)*(rate*0.5));

//Calculate pay and deductions
grossPay = hoursWorked * rate;

by this:

// Calculate base salary
grossPay = hoursWorked * rate;

// Calculate Overtime
if (hoursWorked > 80)
grossPay+=((hoursWorked-80)*(rate*0.5));

0

Commented:
Its not clear what is "=.5" and  what is "= 0"  Ill cal that "overtime"

Then you woudl have somthing like

double OverTime;
double Hours;

// set hours.

if (Hours > 80)
Overtime = 0.5;
else
Overtime = 0;

Let me know if you have any questions.
0

Commented:
Note that when hours = 80, it went with the Overtime=0.  That might not be what you want.
0

Commented:
What is the logic U are trying to make?

Is it :
if(Current_time-Givven_time) > 80 then
retcode = 0.5
else
retcode = 0
endif

?????
0

Author Commented:
it's not increasing the overtime pay to .5
please tell me what i'm doing wrong.
//variable
double oT              = 0;
// if
if (hoursWorked > 80) oT = 0.5;
else oT = 0;
// calculate
grossPay = hoursWorked * rate * oT;

0

Commented:

Got U,

What U need is this:

float HoursWorked,GrossPay;

GrossPay=HoursWorked*SalaryPerHour;
if (HoursWorked > 80)
GrossPay+=((HoursWorked-80)*
(SalaryPerHour*0.5));

Have Fun

0

Commented:
Right, or another way of looking at it,

grossPay = hoursWorked * rate * oT;

can't be right when oT is 0 or .5.  if oT is 0, then the pay will be 0, becayse ou are multiplying by 0.   So what you want instead is 1 and 1.5, like

double oT   = 0;
if (hoursWorked > 80) oT = 1.5;
else oT = 1;

grossPay = hoursWorked * rate * oT;
0

Author Commented:
I'm not getting an increasment in my rate after 80 hours.

here's what I have so far.  Aslo I don't have any errors.void main ()
{
//Define Variables

char lastName[10];
char firstName[10];
double hoursWorked = 0;
double rate;
double deduction   = .425;
double grossPay;
double netIncome;

//Enter Input Items

cout <<"Enter your last name: ";
cin >> lastName;
cout <<"Enter your first name: ";
cin >> firstName;
cout << "Enter hours worked: ";
cin >> hoursWorked;
cout << "Enter pay rate: ";
cin >> rate;

//If over 80 hours

if (hoursWorked > 80)
grossPay=((hoursWorked-80)*(rate*0.5));

//Calculate pay and deductions
grossPay = hoursWorked * rate;
deduction = (deduction * grossPay);
netIncome = grossPay - deduction;

0

Author Commented:
how do I give you 20 points and neitoid 10?

(If I had more points I would give them to you.)
0

Commented:
You can't.  But I really haven't been alll that helpful--I don't think at least.  (sasson's timing is better.  :-) )  I'll withdraw my answer and then you can accept one of his comments as the answer.
0

Commented:
grossPay = (houseWorked>80?1.5:1.0)*rate;
0

Commented:
True, although as sasson has pointed out the first 80 hours is probably calculated at 1* and then any remaining hours at 1.5*
0

Commented:
that's what the line above does .. calculates first 80 hours at 1.0 and rest and 1.5.

its a lot simpler than writing a multi-line if for such a simple calc.
0

Commented:
it does?    No.... It says GrossPay to the rate or to 1.5* the rate.  Then presumabely you will multiple by the hours, so you will have all the hours at the 1*rate or all the hours at the 1.5*rate.   Or am I missing something?
0

Commented:
oops .. ok .. how about:

grossPay = rate * (hoursWorked<=80 ? hoursWorked : (80+1.5*(hoursWorked-80));

which 'simplifies'to

grossPay = rate * (hoursWorked<=80 ? hoursWorked : 1.5*hoursWorked-40);

When a conditional loks like this .. its time to write it as a couple of statements for clarity - and remove the use of the constant '80' as much as possible.

double standardHours;
double overtimeHours;
const double maxStandardHours = 80.0
if (hoursWorked <= maxStandardHours) {
standardHours = hoursWorked;
overtimeHours = 0.0;
} else {
standardHours = maxStandardHours;
overtimeHours = hoursWorked-standardHours;
}
grossPay = rate * (standardHours + 1.5*overtimeHours);

There are many variations on this theme.  eg..

const double maxStandardHours = 80.0
double standardHours = (hoursWorked <= maxStandardHours) hoursWorked : maxStandardHours;
double overtimeHours = hoursWorked-standardHours;
grossPay = rate * (standardHours + 1.5*overtimeHours);

Even though there are many more lines in code like this, the result is easier to read and maintain.

And you're less likely to make mistakes like I did earlier :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.