Thread: weekly wage calculator

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    35

    Question 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);
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.

    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.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    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.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    14
    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

    please help

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    14
    please help

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    14
    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

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    int numbers[5];
    int counter;
    
    for( counter = 0; counter < 5; counter++)
    {
       cout << "enter value #" << counter << ": ";
       cin >> numbers[counter];
    }

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    14
    so would i need 10 for the employee to enter wages, and 10 for hours worked?

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    14
    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;

    cout<<"please enter wage: ";
    cin>>wage1;
    cout<<"Please enter hours worked: ";
    cin>>hours;

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

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



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





    could u show me what u mean?

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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;
    }

  13. #13
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    Quote Originally Posted by snooki View Post
    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

    please help
    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.
    Asking a question you already know the answer to might teach you something you did not know...

  14. #14
    Registered User
    Join Date
    Dec 2009
    Posts
    14
    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.

  15. #15
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Use the explanation I have given a number of times above. Here is my final attempt to help.
    Code:
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help modifying my calculator!!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 03-25-2008, 12:03 PM
  2. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 04:39 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM