Results 1 to 5 of 5

Thread: Finding weekly pay, overtime hours worked? What's wrong with the code?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2014
    Posts
    12

    Finding weekly pay, overtime hours worked? What's wrong with the code?

    So basically hour teacher gave us the private sub, and we had to do the rest from there. For the interface it's the user inputting their hours worked, and their hourly pay. They click a button, then in two textboxes it shows their overtime hours worked, and week's pay.

    For example:
    Input
    Hours worked: 42
    Hourly Pay: 10.00
    Press Button
    Output
    Overtime Hours worked: 2
    Week's Pay: $430.00


    I can't find out what's wrong with my code, any help is appreciated!!




    Code:
    Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
            Dim hours, payPerHour, overtimeHours, pay As Double
            InputData(hours, payPerHour)
            CalculateValues(hours, payPerHour, overtimeHours, pay)
            DisplayData(overtimeHours, pay)
        End Sub
    
        Sub InputData(ByVal hours As Double, ByVal payPerHour As Double)
            hours = CDbl(txtHoursWorked.Text)
            payPerHour = CDbl((txtHourlyPay.Text))
        End Sub
    
        Sub CalculateValues(ByVal hours As Double, ByVal payPerHour As Double, ByVal overtimeHours As Double, ByVal pay As Double)
            If hours <= 40 Then
                overtimeHours = 0
            Else
                overtimeHours = hours - 40
            End If
            pay = (((overtimeHours * payPerHour) * 1.5) + (hours * payPerHour))
        End Sub
    
        Sub DisplayData(ByVal overtimeHours As Double, ByVal pay As Double)
            txtOvertime.Text = CStr(overtimeHours)
            txtWeeksPay.Text = CStr(pay.ToString("C"))
        End Sub

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Finding weekly pay, overtime hours worked? What's wrong with the code?

    Code:
    Sub InputData(ByRef hours As Double, ByRef payPerHour As Double)
    Code:
    Sub CalculateValues(ByVal hours As Double, ByVal payPerHour As Double, ByRef overtimeHours As Double, ByRef pay As Double)
    http://msdn.microsoft.com/en-gb/library/c84t73c2.aspx

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2014
    Posts
    12

    Re: Finding weekly pay, overtime hours worked? What's wrong with the code?

    Oh awesome, simple fix, thanks a lot! Works fine now

  4. #4
    Addicted Member
    Join Date
    Oct 2013
    Posts
    212

    Re: Finding weekly pay, overtime hours worked? What's wrong with the code?

    You still have an error. Pay receives 450 instead of 430
    CalculateValues should be like this:

    Code:
      Sub CalculateValues(ByVal hours As Double, ByVal payPerHour As Double, ByRef overtimeHours As Double, ByRef pay As Double)
            If hours <= 40 Then
                overtimeHours = 0
            Else
               overtimeHours = hours - 40
                hours = hours - overtimeHours
            End If
            pay = (((overtimeHours * payPerHour) * 1.5) + (hours * payPerHour))
        End Sub
    Last edited by Atenk; Jun 12th, 2014 at 03:11 PM.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Finding weekly pay, overtime hours worked? What's wrong with the code?

    I'd use Atenk's code, but modify it slightly:

    Code:
    overtimeHours = math.max(0, hours - 40)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width