 # Thread: Pennies for pay help

1. ## Pennies for pay help

HI, i just start a vb class and i need help with a project. the project is pennies for where the pay rate will be one penny the first day and
two pennies the second day, four pennies the third day and continuing to double each day. now i got it to work for only 3 days but when i get the 4th day it doesn't calculate any more. Can someone help me, I`ve been at it for 2 weeks and i can not figure it out, it due tomorrow and I`m driving myself crazy. here is the code:

Private Sub bntCal_Click(sender As Object, e As EventArgs) Handles bntCal.Click

Const decPenny As Decimal = 0.01

Dim intDaysWorked As Integer
Dim dblTotalPay As Double = 0
Dim decLastPay As Decimal

Integer.TryParse(txtDaysWorked.Text, intDaysWorked)
Double.TryParse(lblTotalPay.Text, dblTotalPay)
Decimal.TryParse(lblTotalPay.Text, decLastPay)

If (intDaysWorked <= 2) Then

dblTotalPay += (intDaysWorked * decPenny)

lblTotalPay.Text = dblTotalPay.ToString("C2")

Else
If (intDaysWorked >= 3) Then

intDaysWorked = 2
decLastPay += (intDaysWorked * decPenny)
dblTotalPay += (decLastPay * 2)

lblTotalPay.Text = dblTotalPay.ToString("c2")

End If
End If

End Sub

Basically this is what I'm getting so far:

day 1 = 0.01
day 2 = 0.02
day 3 = 0.04

next day 4 should equal 0.08 but it gets stuck at 0.04 and at anything above day 4.  Reply With Quote

2. ## Re: Pennies for pay help

What is the exercise supposed to teach you? Classes tend to cover various topics. If you are learning looping, then the solution should involve looping. If looping isn't the point, then there are other solutions, but what IS the point?

This seems to be an excersize in looping, in which case there should be a loop in there, and you don't have one. With a loop, the code would do these things:

1) Get the number of days.
2) For x = 0 to the number of days minus one, get the pay for each day and add it to a running total.

The pay for each day would just be 2^x cents. To turn that into dollars, you'd just do (2^x)/100.

Why are you converting the total pay from a label into a double? Isn't the total pay the output that you want to display? Is there already some pay amount in the label that you have to add the new pay to?  Reply With Quote

#### Posting Permissions

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

Featured