The assignment is to design a program that determines the gross pay for each employee.

Everything seems to work, but when you input more than 40 hours, it doesn't calculate overtime.

Another thing is that, i can't get a message to pop up when someone input an invalid input. I can use an invalid exception, but I want to know why the if statement isn't working.

class OverTime
{
public:
    OverTime (float = 0, float = 0);

    void sethoursWork(float);
    void sethourlyRate(float);

        float gethoursWork();
    float gethourlyRate();
    float getsalary();

private:
    float hoursWork;
    float hourlyRate;
};

#include "stdafx.h"
#include "OverTime.h"

OverTime::OverTime(float a, float b)
{
    hoursWork = a;
    hourlyRate = b;
}

void OverTime::sethoursWork(float hW)
{
    if (hoursWork > 40)
    {
        hourlyRate += (hourlyRate /2);
    }
    else
      hoursWork = hW;
}

void OverTime::sethourlyRate(float hR)
{
  if (hourlyRate <=0)
    {
      cout << "Please input greater than zero.";
    }
    else
      hourlyRate = hR;
}

float OverTime::gethoursWork()
{
    return hoursWork;
}

float OverTime::gethourlyRate()
{
    return hourlyRate;
}

float OverTime::getsalary()
{
    return hoursWork * hourlyRate;
}

#include "stdafx.h"
#include "OverTime.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    float hoursWork1;
    float hourlyRate1;

    char answer;
    answer = 'y';

    OverTime overTime(0);
    do
    {
        cout << "Enter hours work: ";
        cin >> hoursWork1;

        cout << "Enter hourly rate: ";
        cin >> hourlyRate1;

        overTime.sethoursWork(hoursWork1);
        overTime.sethourlyRate(hourlyRate1);

        cout << "Your Salary is: " << overTime.getsalary() << endl;
        cout << "\n";

        cout << "Do you want to enter another?";
        cin >> answer;
        cout << "\n";

    } while (answer == 'y');
    return 0;
}

Everything seems to work, but when you input more than 40 hours, it doesn't calculate overtime.

You make no attempt to calculate overtime.

float OverTime::getsalary()
{
	return hoursWork * hourlyRate;
}

Another thing is that, i can't get a message to pop up when someone input an invalid input. I can use an invalid exception, but I want to know why the if statement isn't working.

void OverTime::sethourlyRate(float hR)
{
  if (hourlyRate <=0)
    {
      cout << "Please input greater than zero.";
    }
    else
      hourlyRate = hR;
}

Look at the variable names in lines 1 and 3 above. They aren't the same. If your function is passed -1, it's not checking whether -1 is less than 0.

You make no attempt to calculate overtime.

float OverTime::getsalary()
{
	return hoursWork * hourlyRate;
}

OK, I guess I see where you make the attempt. You change hourlyRate here:

void OverTime::sethoursWork(float hW)
{
	if (hoursWork > 40)
	{
		hourlyRate += (hourlyRate /2);
	}
	else
	  hoursWork = hW;
}

You have the same problem as I pointed out in my last post. Look at the variable names in lines 1 and 3.

Also, think about the logic here. If I get paid $10 an hour and work 50 hours, I should get paid $550, right? $10 per hour for the first 40 hours, $15 per hour for the last 10.

You make no attempt to calculate overtime.

float OverTime::getsalary()
{
	return hoursWork * hourlyRate;
}
void OverTime::sethourlyRate(float hR)
{
  if (hourlyRate <=0)
    {
      cout << "Please input greater than zero.";
    }
    else
      hourlyRate = hR;
}

Look at the variable names in lines 1 and 3 above. They aren't the same. If your function is passed -1, it's not checking whether -1 is less than 0.

void OverTime::sethoursWork(float hW)
{
         if (hoursWork > 40)
        {
                hourlyRate += (hourlyRate /2);
        }
        else
         hoursWork = hW;
}

Doesn't this set the overtime? I tried is the if condition in the getsalary() and comes up with errors.

float OverTime::getsalary()
{
	if (hoursWork > 40)
	{
		hourlyRate += (hourlyRate /2);
	}
	else

	return hoursWork * hourlyRate;
}
float OverTime::getsalary()
{
	if (hoursWork > 40)
	{
		hourlyRate += (hourlyRate /2);
	}
	else

	return hoursWork * hourlyRate;
}

Problem with the above function is you have an else clause you don't need. In the above, not all paths return a value because the return statement is part of your else clause only, but the function expects you to return a float regardless of the logical paths.

Problem with the above function is you have an else clause you don't need. In the above, not all paths return a value because the return statement is part of your else clause only, but the function expects you to return a float regardless of the logical paths.

That code will not compile

That code will not compile

First : DdoubleD was pointing out a flaw, he wasn't giving a suggestion.
Second: Why wouldn't this compile?

Thank you!I really appreciate ur help!

First : DdoubleD was pointing out a flaw, he wasn't giving a suggestion.
Second: Why wouldn't this compile?

Well I use compiler with "treat warning as error option" , are you not getting any warning in the line "else " from your compiler ? what compiler do you use ?

Well I use compiler with "treat warning as error option" , are you not getting any warning in the line "else " from your compiler ? what compiler do you use ?

My compiler indeed gives a warning because not all return paths return a value. But that is not something a compiler should normally quit on. Except when you set the "treat warning as error option". I didn't say that is was good code, but it will compile.

That code will not compile

I posted the original code and pointed out the problem area within the function as I saw it. I also use treat warnings as errors. I didn't try to compile the function because I didn't need too.

Cheers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.