Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# If statement

Posted on 2000-04-06
Medium Priority
202 Views
how do I do an if statement when > 80 hours = .5 ( time and a half ) else < 80 = 0
0
Question by:nationnon
• 6
• 3
• 3
• +1

LVL 22

Expert Comment

ID: 2690013
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

LVL 22

Expert Comment

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

Expert Comment

ID: 2690031
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

LVL 1

Author Comment

ID: 2690572
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

Expert Comment

ID: 2690613

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

LVL 22

Expert Comment

ID: 2690824
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

LVL 1

Author Comment

ID: 2690866
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

Accepted Solution

sasson earned 120 total points
ID: 2690889
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

LVL 1

Author Comment

ID: 2690961
how do I give you 20 points and neitoid 10?

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

LVL 22

Expert Comment

ID: 2690969
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

LVL 10

Expert Comment

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

LVL 22

Expert Comment

ID: 2693004
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

LVL 10

Expert Comment

ID: 2695077
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

LVL 22

Expert Comment

ID: 2695963
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

LVL 10

Expert Comment

ID: 2696027
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more adâ€¦
Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and â€¦
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relatâ€¦
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month15 days, 4 hours left to enroll