### HELP: C++ program for calculating overtime payment of an employee.

Please refer to this image: http://i42.tinypic.com/5ed0yc.png

Hope you don't mind not posting the content here.

I'm a beginner and know little up to arrays and pointers. Thank you ever so much for anyone who could help.
did you learn functions?
 ``123456789101112131415161718192021222324`` ``````#include using namespace std; int main() { cout << "Enter grade: "; int grade = 0; cin >> grade; cout << "Enter no of overtime hours: "; int otHours = 0; cin >> otHours; int otPayment = 0; int aRate [2][4] = {{0, 100, 150, 175}, {0, 80, 100, 125}}; if (otHours > 100 && otHours < 121) otPayment += aRate[grade-1][1] * (otHours - 100); if (otHours > 120 && otHours < 151) otPayment += aRate[grade-1][1] * 20 + aRate[grade-1][2] * (otHours - 120); if (otHours > 150) otPayment += aRate[grade-1][1] * 20 + aRate[grade-1][2] * 30 + aRate[grade-1][3] * (otHours - 150); cout << "Payment (Rs.): " << otPayment << endl; return 0; } ``````

My code works, but I'm not happy with the efficiency, because we had a similar question with around 10 rows to be completed within 20-30 minutes in my previous exam.

@TinyTeeTree: Yup we did learn functions.
Last edited on
lol if it works, then did you come here to flex your programming skills?

you could save lines of code here by mass declaration like this.
`int grade, otHours, otPayment;`

you could concatinate assignment if you want, even though its not necessary to initialize, but it is good practice, which i do too.
`grade = otHours = otPayment = 0;`

and if im not mistaken you you can chain inputs too.

 ``12`` ``````cout << "enter grade and then overtime hours"; cin >> grade >> otHours;``````
No TinyTeeTree, you have gotten me completely wrong.

I'm looking for a short code. because I can't implement the above code for a similar table with around 10 rows (ot hours ranges)
closed account (2U7GNwbp)
I understand what you're saying Reversiblean. I opened your picture and wrote a program myself without reading the rest of the thread first. I came up with almost the exact same thing and I too feel there is much better way to accomplish this. If the table extended your if/else tree will grow to an unwieldy size.

I'm going to play around a bit. I'm also just learning some new concepts in C++. I come from a non-OOP PHP/MySql background and am used to doing things in a very linear fashion, much like the code we both came up with.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include #include using namespace std; int grade; int hours; int outputHours(int, int); int main(int argc, char ** argv) { cout << "Enter GRADE of employee: "; cin >> grade; cout << endl; cout << "Enter NO. of OVT hours: "; cin >> hours; cout << endl; int payment = outputHours(grade, hours); cout << "Payment: " << payment; getch(); return 0; } int outputHours(int grade, int hours) { int pay; if(hours > 100 && hours < 121) { if(grade == 1) { pay = (hours - 100) * 100; } else { pay = (hours - 100) * 80; } } else if(hours > 120 && hours < 151) { if(grade == 1) { pay = ((hours - 120) * 150) + (100 * 20); } else { pay = ((hours - 120) * 100) + (80 * 20); } } else if(hours > 150) { if(grade == 1) { pay = ((hours -150) * 175) + (150 * 30) + (100 * 20); } else { pay = ((hours -150) * 125) + (100 * 30) + (80 * 20); } } return pay; }``````

Let me know if you come across anything!
so what your saying is that you need a formula that you can put in a loop or something?

if so let me try.

 ``12345678910111213141516171819202122232425`` ``````//the array size x containing the overhours int hours[x] = { input , input , input , , , }; //the array size x containing the rates for each overhour int rates[x] = { input , input , input , , , }; // How Many Hours Employee Worked. int hmhew = input; int sum = 0; //how much money the employee earns int amount = 0; //temporary holder for hours per rate employee worked. for(int i=x-1; i>=0; --i){ amount = hmhew - hours[i]; if( amount > 0 ){ sum += amount*rates[i]; hmhew -= amount; } } //sum will now hold how much employee needs to get paid ``````
Last edited on
closed account (2U7GNwbp)
TinyTeeTree,

In your code, you're not accounting for the grade of the employee. 1 or 2. The rates depend on the rate of the employee.
closed account (2U7GNwbp)
Okay I think I got it. This should scale just fine. All you have to do is change the values in the arrays accordingly. Don't forget to change the size of the payScale array if that's how you want to declare it. I tried to comment as best as I could. Let me know if you have any questions.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100`` ``````///////////////////////////////////////////////////////////////////////// // Employee Overtime Pay Calculator | by Owlie53 | // ///////////////////////////////////////////////////////////////////////// #include #include #include using namespace std; // This initializes the array that stores all of out overtime hour points // Then loads it in to a Vector so we can have the control we need over it // This is a good solution if you don't want to use other outside libraries like Boost, etc // This array holds all of hour overtime hour tiers static const int otHoursArray[] = {100, 120, 150}; // It is then converted in to a vector since we need to know the size // There are a few ways to accomplish this. I chose converting to a vector. // This also saves us the time of declaring a vector, and then pushing each value in. vector otHours(otHoursArray, otHoursArray + sizeof(otHoursArray) / sizeof(otHoursArray[0])); // Multidimensional Employee Pay Scale Array int payScale[3][3] = { {}, // We leave this array blank to make accessing the payscales more literal and more simple {100, 150, 175}, // Rank 1 Employee Pay Scale {80, 100, 125} // Rank 2 Employee Pay Scale }; // This is the declaration for our overtime calculator function // It will be pass the RANK of the employee, and the amount of OVERTIME HOURS accrued int calc(int grade, int hours); // Variable Initialization int grade; // Employee Grade Identifier. This will correspond to our payScale array int hours; // The total number of hours the employee has worked int main(int argc, char ** argv) { // The first thing we need to do is get the user input cout << "Enter employee grade (1 | 2): "; cin >> grade; cout << endl; cout << "Enter employee work hours: "; cin >> hours; cout << endl; // Now let's pass these values to our calculator function and get our output cout << "Total Overtime Payment: \$" << calc(grade, hours) << ".00" << endl; getch(); return 0; } int calc(int grade, int hours) { int totalPayout = 0; // This will hold our number for the total amount being paid to the employee int tempHold; // This will hold a temp value to keep track of hour hours // Since we know we're not paying out for any hours under 'minHours' // We can immediately subtract minimum ammount of hours needed to rate overtime from the total hours hours -= otHours[0]; // Subtract the first element in our otHours array (the minimum hours needed) from the total hours worked. // If the amount of hours worked is now zero or less, specify that no payment is due if(hours < 1) { return 0; } else { // Now lets break down the hours and pay out accordingly // Initialize a counter int count = 0; // While we're within the scope of hour overtime pay scale while(hours > 0 && count < (otHours.size() - 1)) { // Set tempHold to the difference between the pay scales tempHold = (otHours[count + 1] - otHours[count]); //cout << endl << tempHold << endl; if(hours >= tempHold) { totalPayout += tempHold * (payScale[grade][count]); hours -= tempHold; //cout << endl << payScale[grade][count] << " | " << totalPayout << " | " << hours << endl; } else if (hours < tempHold) { totalPayout += hours * (payScale[grade][count]); hours -= tempHold; //cout << endl << payScale[grade][count] << " | " << totalPayout << " | " << hours << endl; } count++; } // Now that we've calculated the pay for every hour within our payscale // The remainder (anything over the maximum amount of hours given in the array) // Will be calculated and added to the total amount. if(hours > 0) { totalPayout += hours * (payScale[grade][count]); hours -= hours; } return totalPayout; } }``````
Last edited on
Owlie, Big thanks for the time you have spent on me. I don't know how to appreciate you enough, if I could choose a best answer I would choose yours : ) thanks to TTT aswell.
closed account (2U7GNwbp)
Reversiblean,

No problem at all. Like I said I'm making the transition to C++ myself so I'm still learning quite a bit. If you come across anything you can improve on in that code, let me know!
Topic archived. No new replies allowed.