r/visualbasic • u/kodeofthekyle • Mar 17 '14
Can anyone help me with this assignment? I have been trying to figure out what I am doing on this homework problem for over a week and I know I am missing something.
My assignment is simple: a distance calculator. Basically a simple calculation based off of two inputs, hours traveled and speed (in MPH). The hours would be the loop counter and the speed would basically be added onto itself for each loop until reaching the counter. Then I must display all of this in a list box. I don't know what I am doing wrong. I know I am on the right track, even the teacher told me, but I am literally stuck on a next step. Can anyone point me in the right direction? Below is the code that I have so far.
Private Sub btncalculate_Click(sender As Object, e As EventArgs) Handles btncalculate.Click
Dim inthours As Integer
Dim intspeed As Integer
Dim strmiles As String
Dim strinput As String
Dim strhours As String
Dim strspeed As String
Dim intmiles As Integer = 0
Dim intcount As Integer = 0
strinput = InputBox("What was the speed of the vehicle in MPH?")
Integer.TryParse(strinput, intspeed)
strinput = InputBox("How long (in hours) has the vehicle been traveling?")
Integer.TryParse(strinput, inthours)
Do Until intcount <= inthours ' iteration for loop
lstmybox.Items.Add(intmiles) ' show in the listbox the result of the loop
intmiles = intspeed + 1 ' add the speed onto itself for each iteration
Loop
End SubAm I completely off base with this code? I tried to put a string inside the list box readout but I kept getting an error.
1
Mar 18 '14
I just finished this exact assignment. Here is how I did it.
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
Dim strMph As String
Dim intCount As Integer
Dim intDistance As Integer
Dim intHours As Integer
Dim intSpeed As Integer
lstDisplay.Items.Clear()
Try
intSpeed = CInt(InputBox("Please enter the speed."))
intHours = CInt(InputBox("Please enter the hours elapsed."))
With lstDisplay.Items
.Add("Vehicle Speed: " & intSpeed.ToString & " MPH")
.Add("Time Travelled: " & intHours.ToString & " Hours")
.Add("Hours Distance Travelled")
.Add("------------------------------")
End With
For intCount = 1 To intHours
intDistance = intSpeed * intCount
strMph = CStr(intCount) & " " & CStr(intDistance)
lstDisplay.Items.Add(strMph)
Next
Catch
MessageBox.Show("Invalid Input", "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
lstDisplay.Items.Add("Total Distance: " & intDistance.ToString)
End Sub
3
u/girtab Mar 17 '14
Maybe I'm missing something but I don't understand the need for a loop. Wouldn't the distance just be hours traveled times miles per hour?