# weekly wage calculator

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-05-2009
brian75
weekly wage calculator
I am trying to make a program that takes an employees wage and their hours worked and have the output be their money made for that week. Anything over 40 hours is going to be counted for overtime at time and a half.

I can get the program to accuratley output the employees wage up to and including 40 hours but after that I will still only get the output of their wage at 40 hours; how can I get the accurate print out with their overtime hours counted?

As a side question this program only works for whole numbers, how can I get it to work with decimils, ex. 10.74, 12.10, 22.57, etc. as an hourly wage?

The current code is as followed:

Code:

```#include <iostream> int hoursWorked;  //hours worked        int wage1;        //hourly wage int wage2;        //time and a half pay int overtime;    //amount paid with overtime hours int main() {         std::cout << "Please enter your current wage: " ;         std::cin >> wage1 ;  //input of current hourly wage         std::cout << "Please enter your hours worked this week: " ;         std::cin >> hoursWorked ;  //input of hours worked         int regularPay;  //amount made if worked 40hrs         int extraPay;    //amount made if worked anything over 40hrs         regularPay = ( wage1 * 40 ) ;         extraPay = ( hoursWorked - 40 ) * wage2 ;         wage2 = wage1 + ( wage1 / 2 ) ;         overtime = regularPay + extraPay ;  //overtime = amount made at 40hrs + amount made after 40hrs   /* If employee worked 40hrs or under then show the employees income at their regular wage;     and if employee works more than 40hrs show the employees income with their overtime included */         if ( hoursWorked <= 40 )         {                 std::cout << "Your weekley income is " << wage1 * hoursWorked << '\n' ;         }         else         {                 std::cout << "Your weekley income is " << overtime << '\n' ;         }         return (0); }```
• 12-05-2009
Code:

```        regularPay = ( wage1 * 40 ) ;         extraPay = ( hoursWorked - 40 ) * wage2 ;         wage2 = wage1 + ( wage1 / 2 ) ;```
Your using "wage2" without initializing (giving it a value) it first. You need to swap the 2nd and 3rd lines, so that you first assign wage2 something, then assign extraPay.

Quote:

As a side question this program only works for whole numbers, how can I get it to work with decimils, ex. 10.74, 12.10, 22.57, etc. as an hourly wage?
As you said, your numbers are "whole numbers" which are "integers" (they are all the same thing). This is why youre using "int". "decimal" numbers are called "real numbers", or "floating point" numbers. Use "float" datatype for this. So to allow for real numbers as the wage, change all "int" to "float" for any calculation that uses the wages (including the total when you do multiplication or any additions!).

Also, after looking back, it is currently required for "extraPay" and "wage2" to be "float". The only case where it currently would work is if "wage1" is an even number.

I havent looked at the rest of the code, but if there are more problems let us know.
• 12-05-2009
brian75
That made the program work exactly how I wanted it to, getting the decimils and the overtime read out. I cant believe I missed something so simple.

Thank you very much.
• 12-21-2009
snooki
hey, i have recently made a program very similar but now need it to be able to have 10 users input there pay etc and get the outcome like the programme does at the momonet but am unsure as how to do it, or how to get the ball rolling

• 12-21-2009
snooki
• 12-21-2009
Do the same thing, 10 times? Simply have an array of size 10, of all of the variables relating to each employee, and put it in a for loop.
• 12-21-2009
snooki
yeah an array is what i think i need but unsure how to execute, will i need an array for every wage and hour input? could someone give an example of an array
• 12-21-2009
Code:

```int numbers[5]; int counter; for( counter = 0; counter < 5; counter++) {   cout << "enter value #" << counter << ": ";   cin >> numbers[counter]; }```
• 12-21-2009
snooki
so would i need 10 for the employee to enter wages, and 10 for hours worked?
• 12-21-2009
If you want to store 10 wages then of course you need an array of 10 wages. Same for any other variable used.

If you just want to get the input and print it, then not do anything else with it, then you can just use the exact code above, but put it in a for loop, looping 10 times.

For example, if you just want to ask for a wage and hours, then only output the pay, then you just need 3 variables. Then you just put this code in a for loop. On the other hand, if you want to ask for the wage and hours, and do more than just output it (i.e. save it for later), then you need to keep track of each one. So for any variable used, you need an array of 10 of them. So you would have "int hours[10], float wage[10], float pay[10]", etc.
• 12-21-2009
snooki
i understand the principal, just cant figure how to do it.

this is my code:

int main()
{

float hours=0.0,wage1=0.0,pay=0.0,overtime=0.0,overtimep ay=0.0,totalpay=0.0;

cin>>wage1;
cin>>hours;

pay=wage1*35;
overtime=wage1*1.5;
overtimepay=(hours-35)*overtime;
totalpay=pay+overtimepay;

if(hours<=35)
{
}
else
{
cout<<"Your weekley pay + overtime is: "<<totalpay;
}

cin.ignore();
cin.ignore();

could u show me what u mean?
• 12-21-2009
I have no clue what you want. Put all of your code in a for loop
Code:

```//#includes here int main (void) {   int counter;   for ( counter = 0; counter < 10; counter++)   {     // cut/paste your code here   }   return 0; }```
• 12-21-2009
QuestionKing
Quote:

Originally Posted by snooki
hey, i have recently made a program very similar but now need it to be able to have 10 users input there pay etc and get the outcome like the programme does at the momonet but am unsure as how to do it, or how to get the ball rolling

You shouldn't hijack someone's thread, it's not nice. If you have a question, post it and people will help.
Also consider using the code tags for your code and be sure to indent.
• 12-21-2009
snooki
let me try explain again hope i expalin better. I need a programme to calculate 10 employee wages based on there hourly pay (manual enter), hours worked (manual input) and it needs to calculate over time over 35 hours. and then display the pay for each individual, and a total wages for all 10 employees. But i didnt want to put the code in 10 times, thought there may have been a simple loop i could have used.
• 12-21-2009
```int hours[10]; float wages[10]; float pay[10]; int i; for (i = 0; i < 10; i++) {   // ask for wage   // store at wages[i]   // ask for hours   // store at hours[i]   pay[i] = hours[i]*wages[i]; } // now do whatever you want with pay, for example, use a for loop to iterate from 0 to 9, and print each pay, etc.```