C++ Diane Zak Problem Help

Hi guys - I am in an introductory computer programming class and am having difficulties.

Here is my problem and code:

The program should allow the user to enter the number of hours the employee worked and his or her hourly rate. Use a negative sentinel value to stop the program. Employees are paid at their regular pay for hours worked from 1 through 37. They are paid time and one half for hours worked 38 through 50, and double time for hours worked over 50. Use a program defined function to calculate and return the employees overtime pay.

#include<iostream>
#include <iomanip>
using namespace std;

int main()
{
//Delcare named constants and variables
double hoursWorked = 0.0;
double hourlyPay = 0.0;
double grossPay = 0.0;
double overtimeOne = 0.0;
double overtimeTwo = 0.0;
double number = 0.0;

//Enter input
cout << " Enter employee ID: ";
cin >> number;
cout << "Enter the number of hours worked: ";
cin >> hoursWorked;
cout << "Enter the hourly pay: ";
cin >> hourlyPay;

//Calculate gross pay

overtimeOne = ((hoursWorked - 37) *(hourlyPay * 1.5));
overtimeTwo = ((hoursWorked - 51)*(hourlyPay * 2));

if (hoursWorked <= 37 && number <= 0)
{
grossPay = (hoursWorked * hourlyPay);
cout << " The employees gross pay is: " << grossPay << endl;
}
else
if (hoursWorked >= 38 && hoursWorked < 51 && number <= 0)
{
grossPay = ((37 * hourlyPay) + overtimeOne);
cout << " The employees gross pay is: " << grossPay << endl;
}
else
if (hoursWorked >= 51 && number <= 0)
{
grossPay = ((37 * hourlyPay) + (overtimeOne + overtimeTwo + 5));
cout << " The employees gross pay is: " << grossPay << endl;
}
else
if (number < 0)
{
break;
}


//end of main function
system("pause");
return 0;
}


I am having difficulties with the calculations. Time and one half does not calculate correctly even though I think have put it in correctly. I also need some help getting it to loop.

Thanks
I see that you only do any calculations if the employee number is negative or zero. That doesn't seem to make any sense.
I put in employee number as a way to input another employee. I needed a negative value to end the program.
Hello winwin01,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

You have a start to your program. The "Define variables" section is good. The use of doubles is a good choice. Instead you might consider this:

1
2
3
4
5
6
7
8
9
10
double totalHoursWorked = 0.0;
double hourlyPayRate = 0.0;
double grossPay = 0.0;
double overtimeOne = 0.0;
double overtimeTwo = 0.0;
double empId = 0.0;

double regHours{};
double overTimeOneHours{};
double overTimeTwoHours{};


After the input section I would break up total hours worked into the last three variables that I added. This may seem like some extra work, but since the "Calculate gross pay" section will not work I am thinking that if you make this two steps it will help you understand before you try to do everything at once.

Give a total of 60 hours worked regular hours would be 37 and over time one hours would be 13 leaving over time two hours with 9. Now that you have something to start with make the program come up with those numbers first before thinking about figuring gross pay.

Most times it is better to work on small pieces than the whole.

Hope that helps,

Andy
So why do you only do calculation when the employee number is negative?

if I enter the details of employee number 5, I want to know the pay for employee five. But your code won't calculate that because five is not zero or less.

I think you should do this in parts. First, make a program that calculate the pay.

Then, when that's working, add something to make it stop if you enter a negative number. Make them completely different.

Right now, your code only does calculations if the user enters a negative number, which makes no sense at all.
You're missing Repeater's point. Your if statements only execute if number is <= 0 which is the opposite of what you want.

Your calculation of double time is flawed. If an employee works more than 51 hours, the double time hours are included in your calculation of overtimeOne.

What happens if an employee works 37.5 hours? What happens to the 1/2 hour of overtime?

Your problem statement says "Use a program defined function to calculate and return the employees overtime pay.". I see no such function.

Regarding your loop:
1
2
3
4
   do
   {  // logic 
    }
   while (number > 0);


PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Topic archived. No new replies allowed.