Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

If statement

Posted on 2000-04-06
15
Medium Priority
?
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 

Accepted Solution

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

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

604 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