Solved

If statement

Posted on 2000-04-06
15
185 Views
Last Modified: 2010-04-02
how do I do an if statement when > 80 hours = .5 ( time and a half ) else < 80 = 0  
0
Comment
Question by:nationnon
  • 6
  • 3
  • 3
  • +1
15 Comments
 
LVL 22

Expert Comment

by:nietod
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

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

Expert Comment

by:sasson
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

by:nationnon
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

by:sasson
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

by:nietod
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

by:nationnon
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Accepted Solution

by:
sasson earned 30 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

by:nationnon
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

by:nietod
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

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

Expert Comment

by:nietod
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

by:RONSLOW
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

by:nietod
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

by:RONSLOW
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now