Hi,
Thank you for the videos and the great lessons. I Just finished the assignment and was looking through your code. It seems this program adds an additional cost of $20 to orders over 100′ rather than giving a discount.

Also why add intCost (which has a value of zero at this point) to intSegments? As in this line:
intCost = intCost + intSegments * 40 ? Is there some subtlety in vba why this is a good idea?

If you input a number less than 20′ it returns that 2 segments are required.

May I suggest this code:

Option Explicit
Dim strMaterial As String
Dim intCost As Integer
Dim intTubeLength As Integer
Dim intSegments As Integer

Sub main()
intCost = 0
intSegments = 0

‘User defined values
intTubeLength = 99
strMaterial = “Copper”

‘Check if the tube length is positive
If intTubeLength <= 0 Then
MsgBox "Not a valid length"
Exit Sub
End If

Select Case strMaterial
Case "PVC"
intCost = 20
Case "Copper"
intCost = 50
Case "Stainless"
intCost = 60
Case "Titanium"
intCost = 100
Case Else
MsgBox "Type of material not recognized"
Exit Sub
End Select

intSegments = Round(intTubeLength \ 20 + 1, 0)

If intTubeLength < 100 Then 'Extra charge for less than 100'
MsgBox "The total number of segments is " & intSegments & vbNewLine & "Total Cost = $" & intSegments * intCost + 20
Else
MsgBox "There is a discount for orders over 100'" _
& vbNewLine & "The total number of segments is " & intSegments & vbNewLine & "Total Cost = $" & intSegments * intCost
End If

‘user defined values
intTubeLength = 1000
strMaterial = “Titanium”

‘determine number of segments
intSegments = intTubeLength \ 20
If intSegments < 1 And intTubeLength 0 Then
intSegments = 1
End If

If intTubeLength Mod 20 0 Then intSegments = intSegments + 1

‘find cost based on material and number of segments
Select Case strMaterial
Case “PVC”
intCost = intCost + intSegments * 40
Case “Copper”
intCost = intCost + intSegments * 50
Case “Stainless steel”
intCost = intCost + intSegments * 60
Case “Titanium”
intCost = intCost + intSegments * 100
Case Else
MsgBox “Material not recognized.”
Exit Sub
End Select

‘discount applies to orders of over 100 feet
If intTubeLength > 100 Then
intCost = intCost – (10 * intSegments)
blnDiscount = True
End If

‘display results
MsgBox “The total cost is: $” & intCost & vbCrLf & “Segments required: ” _
& intSegments & vbCrLf & “Discount applied: ” & blnDiscount
End Sub

AmitNaraSir,

I have some questions regarding this program:

1) What is the purpose of this line?

If intTubeLength Mod 20 0 Then intSegments = intSegments + 1

2) Cost = No. of Segments * Cost/Segment, then why you use “intCost = intCost + intSegments * 40″.

3) When I use intTubelength = 19. It gives me “Segment Required = 2″

It must be 1 according to Statement of question.

adamyarsHi,

Thank you for the videos and the great lessons. I Just finished the assignment and was looking through your code. It seems this program adds an additional cost of $20 to orders over 100′ rather than giving a discount.

Also why add intCost (which has a value of zero at this point) to intSegments? As in this line:

intCost = intCost + intSegments * 40 ? Is there some subtlety in vba why this is a good idea?

If you input a number less than 20′ it returns that 2 segments are required.

May I suggest this code:

Option Explicit

Dim strMaterial As String

Dim intCost As Integer

Dim intTubeLength As Integer

Dim intSegments As Integer

Sub main()

intCost = 0

intSegments = 0

‘User defined values

intTubeLength = 99

strMaterial = “Copper”

‘Check if the tube length is positive

If intTubeLength <= 0 Then

MsgBox "Not a valid length"

Exit Sub

End If

Select Case strMaterial

Case "PVC"

intCost = 20

Case "Copper"

intCost = 50

Case "Stainless"

intCost = 60

Case "Titanium"

intCost = 100

Case Else

MsgBox "Type of material not recognized"

Exit Sub

End Select

intSegments = Round(intTubeLength \ 20 + 1, 0)

If intTubeLength < 100 Then 'Extra charge for less than 100'

MsgBox "The total number of segments is " & intSegments & vbNewLine & "Total Cost = $" & intSegments * intCost + 20

Else

MsgBox "There is a discount for orders over 100'" _

& vbNewLine & "The total number of segments is " & intSegments & vbNewLine & "Total Cost = $" & intSegments * intCost

End If

End Sub

KeithThanks Adam, these fixes will be implemented when the course is updated.

dodleor you could do something like this and give them a discount for each segment you will need

Option Explicit

Dim intCost As Integer

Dim intTubeLength As Integer

Dim intSegments As Integer

Dim strMaterial As String

Dim blnDiscount As Boolean

Sub main()

‘set initial values

intCost = 0

intSegments = 0

blnDiscount = False

‘user defined values

intTubeLength = 1000

strMaterial = “Titanium”

‘determine number of segments

intSegments = intTubeLength \ 20

If intSegments < 1 And intTubeLength 0 Then

intSegments = 1

End If

If intTubeLength Mod 20 0 Then intSegments = intSegments + 1

‘find cost based on material and number of segments

Select Case strMaterial

Case “PVC”

intCost = intCost + intSegments * 40

Case “Copper”

intCost = intCost + intSegments * 50

Case “Stainless steel”

intCost = intCost + intSegments * 60

Case “Titanium”

intCost = intCost + intSegments * 100

Case Else

MsgBox “Material not recognized.”

Exit Sub

End Select

‘discount applies to orders of over 100 feet

If intTubeLength > 100 Then

intCost = intCost – (10 * intSegments)

blnDiscount = True

End If

‘display results

MsgBox “The total cost is: $” & intCost & vbCrLf & “Segments required: ” _

& intSegments & vbCrLf & “Discount applied: ” & blnDiscount

End Sub

dylankuhafa9Really appreciate the tutorials.

Dim intNumsegments As Integer

Dim intCost As Integer

Dim strPipematerial As String

Dim intReqLength As Integer

Sub main()

strPipematerial = “Stainless Steel”

intReqLength = 105

intNumsegments = intReqLength \ 20

If intReqLength <= 20 Then

intNumsegments = 1

ElseIf intReqLength Mod 20 0 Then

intNumsegments = intNumsegments + 1

End If

Select Case strPipematerial

Case “PVC”

intCost = 40 * intNumsegments

Case “Copper”

intCost = 50 * intNumsegments

Case “Stainless Steel”

intCost = 60 * intNumsegments

Case “Titanium”

intCost = 100 * intNumsegments

End Select

If intReqLength < 100 Then

intCost = intCost + 20

End If

MsgBox "Material: " & strPipematerial & vbCrLf & "Cost :" & intCost

End Sub