Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why is my if else statement not exciting the holiday hrs?

+1 vote
asked Jan 25 by CS50Starter (130 points)

#include <iostream>
#include <iomanip> // decimal places round off
#include <conio.h> // for _getch()
float reg_wages, overtime, double_overtime, holiday_wages, birthdaybonus_wages, 
total_hrs, total_wages;
const float std_hrs = 40.0;


using namespace std;
int main()
{   
    float hrs_worked;// attain total hrs worked
    cout <<"Enter hours worked for the week: ";
    cin >> hrs_worked;
    
    float hrly_rate;//attain regular hourly rate
    cout <<"Enter hourly rate: $";
    cin >> hrly_rate;
    
    float holiday_hrs;//attain amount of holiday hrs
    cout << "How many hours were holiday hours? ";
    cin >> holiday_hrs;
    
    
    //maths
    if (hrs_worked >= std_hrs)//regular + holiday hrs
        {total_wages = (hrs_worked - holiday_hrs) * hrly_rate + 
                       (holiday_hrs *1.5 * hrly_rate);}// works like a dream
        
    else if (hrs_worked >= 40 && hrs_worked <= 60)//overtime 
        {total_wages = ((hrs_worked - std_hrs) + holiday_hrs) * 1.5 * hrly_rate + ‚Äč// here ?! 
                       (std_hrs - holiday_hrs) * hrly_rate;}
    
    else if(hrs_worked >=60 && hrs_worked <=80)// double overtime
        {total_wages = (hrs_worked - std_hrs) * 2 * hrly_rate + 
                       ((hrs_worked - std_hrs) + holiday_hrs) * 1.5 * hrly_rate +// also here?!
                       (std_hrs - holiday_hrs) * hrly_rate;}
 

If I take out the holiday hrs, it factors in the overtime easily. But when I construct it to add them, it just gives me the overtime and skips holiday hrs in the calculation. Even when I add the same method of calculation: (holiday_hrs *1.5 * hrly_rate); to the conditions block it still gets ignored. I am new to C++ so go easy on me. 

2 Answers

0 votes
answered Jan 27 by MattiaGit (140 points)
if hrs_worked >= std_hrs, the first "If" statement is True, and the other "else if" are ignored.

Maybe in the first statement you mean: if(hrs_worked <= std_hrs) ?
0 votes
answered Jan 28 by Peter Minarik (19,180 points)

I'm not sure on the correctness of your maths. I would calculate things differently. But let's put this aside and assume that your maths are correct. Your code is still not.

if (hrs_worked >= std_hrs) { /* ... */ } // regular + holiday hrs
else if (hrs_worked >= 40 && hrs_worked <= 60) { /* ... */ } // overtime
else if (hrs_worked >=60 && hrs_worked <=80)  { /* ... */ } // double overtime

The above code of yours covers the various cases. However, it's wrong. Let's see some problems:

  1. You do not cover cases, when
    1. hrs_worked < std_hrs
    2. hrs_worked > 80
  2. You have multiple cases for the scenarios, when
    1. hrs_worked == std_hrs
    2. hrs_worked == 60

I think the first condition of yours should have been

if (hrs_worked <= std_hrs) { /* ... */ } // regular + holiday_hrs

The multiple cases can be sorted out by not checking equality in both the 2nd and 3rd conditions.

And last, but not least, I'd not set a high end limit (80) for double overtime.

Here's my take on the problem's solution:

#include <algorithm>
#include <iostream>

float reg_wages, overtime, double_overtime, holiday_wages, birthdaybonus_wages, total_hrs, total_wages;

const float std_hrs = 40.0;
const float overtime_hrs = std_hrs / 2;

using namespace std;

int main()
{   
    float hrs_worked;// attain total hrs_worked
    cout <<"Enter hours worked for the week: ";
    cin >> hrs_worked;
    
    float hrly_rate;//attain regular hourly rate
    cout <<"Enter hourly rate: $";
    cin >> hrly_rate;
    
    float holiday_hrs;//attain amount of holiday hrs
    cout << "How many hours were holiday hours? ";
    cin >> holiday_hrs;
    
    total_wages = 0;

    if (holiday_hrs > 0) // holidays worked
    {
        total_wages += holiday_hrs * 1.5 * hrly_rate;
        hrs_worked -= holiday_hrs; // deduce the number of holidays from the hours worked. We've already payed for them.
    }
    
    if (hrs_worked < std_hrs) // standard hours worked
    {
        total_wages += hrs_worked * hrly_rate;
    }
    else // standard hours worked + ...
    {
        total_wages += std_hrs * hrly_rate;
        hrs_worked -= std_hrs; // deduce the standard hours. We've already paied for them.

        if (hrs_worked < overtime_hrs) // ... overtime worked
        {
            total_wages += hrs_worked * 1.5 * hrly_rate;
        }
        else // ... overtime worked + ...
        {
            total_wages += overtime_hrs * 1.5 * hrly_rate;
            hrs_worked -= overtime_hrs;  // deduce the overtime hours. We've already paied for them.
            total_wages += hrs_worked * 2 * hrly_rate;
        }
    }

    std::cout << "total wage: " << total_wages << std::endl;

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...