jenny_may
Member
- Joined
- Aug 20, 2011
- Messages
- 7
- Programming Experience
- Beginner
I'm trying to make a carpet calculator that will do the following:
1) How many metres of carpet are
needed to cover a room.
2) How many rolls of carpet that
will be.
3) How much full-width carpet
will be left on the last roll.
The carpet is supplied in rolls that are 5 meters wide and 30 metres long. Any excess width is discarded. This means that rooms that are wider than 5 meters need several strips of carpet. The smallest space that will be carpeted is 1m x1m, the biggest is 20m x 20m. I only need whole meter lengths, so example if only 1.6meters of carpet were used to cover a room it would be rounded to 2 meters.
Here's the code that's giving me incorrect calculations. The number of rolls required to carpet the room seems to be the biggest miscalculation. I'm at my wits end with this problem.
1) How many metres of carpet are
needed to cover a room.
2) How many rolls of carpet that
will be.
3) How much full-width carpet
will be left on the last roll.
The carpet is supplied in rolls that are 5 meters wide and 30 metres long. Any excess width is discarded. This means that rooms that are wider than 5 meters need several strips of carpet. The smallest space that will be carpeted is 1m x1m, the biggest is 20m x 20m. I only need whole meter lengths, so example if only 1.6meters of carpet were used to cover a room it would be rounded to 2 meters.
Here's the code that's giving me incorrect calculations. The number of rolls required to carpet the room seems to be the biggest miscalculation. I'm at my wits end with this problem.
VB.NET:
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim width, length As Single 'Defines variables.
If IsValidInput(txtWidth.Text) Then
width = txtWidth.Text
End If
If IsValidInput(txtLength.Text) Then
length = txtLength.Text
End If
CalculateAndDisplay(width, length)
End Sub
Sub CalculateAndDisplay(ByVal width, ByVal length)
Dim strips, totalLength, rolls, carpetLeft As Integer 'Defines variables.
Const rollwidth = 5 'Sets the constant amount of the variable"
Const rollLength = 30 'Sets the constant amount of the variable"
strips = width / rollwidth
If (width Mod rollwidth) > 0 Then strips = strips + length
totalLength = strips * length
rolls = totalLength / rollLength
txtMetersRequired.Text = totalLength.ToString()
txtRollsRequired.Text = rolls.ToString()
txtWidthRemaining.Text = carpetLeft.ToString()
If (totalLength Mod rollLength) > 0 Then
txtMetersRequired.Text = totalLength.ToString()
txtRollsRequired.Text = rolls.ToString()
txtWidthRemaining.Text = carpetLeft.ToString()
Else : carpetLeft = 0
End If
End Sub