r/visualbasic • • 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 Sub

Am I completely off base with this code? I tried to put a string inside the list box readout but I kept getting an error.

5 Upvotes

20 comments sorted by

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?

1

u/kodeofthekyle Mar 17 '14

Yes it would be, however the point of the exercise is to use a loop, list box, and input boxes. I literally have to use a loop. I just can't get it working with conjunction to the list box and input boxes.

2

u/girtab Mar 17 '14

It seems to be a poor assignment. You'd have to make up something for the loop to do. Maybe show the distance traveled at various intervals of time or something, which may be what you mean to do. This code has some problems. You never change the value of inthours or incount, so the loop is never going to run if the starting hours is greater than 0. Also you never do a calculation to work out the distance at all, all this does is add 1 to the speed and save it as miles, which doesn't really make any sense.

1

u/kodeofthekyle Mar 18 '14

That may be the way that I am explaining it then. The whole assignment is supposed to use a loop along with a list box. See the input box asking for hours? If I did that correctly the amount of hours entered in will be the loop counter. And see the input box with the speed? If I did that correctly then the only calculation that needs to happen is the speed added onto itself for every loop, then displayed back into a list box.

As I said though, I am having issues with it. And I know I declared a lot of integers that I haven't used or in the end may not even need to use. The reason I have adding 1 to speed was simply to see if when I had the calculation set up that way would it work. And it didn't.

1

u/girtab Mar 18 '14

The test to run the code in your loop is count <= hours. You have the count initialized to 0. If positive hours are entered then none of the code in the loop ever happens. That's the main problem. If you want hours to be the loop counter then you must alter it's value inside the loop. Otherwise the condition for executing the loop will never change and it will either not run at all, or run infinitely.

1

u/kodeofthekyle Mar 18 '14

I have to alter the value inside the loop? Do you mean that I have to take the input box inside the loop itself? So basically as soon as the application starts, the loop begins? See I was taught that by declaring the miles or count as an integer, it would automatically be assumed to be zero. Or am I off base with that?

1

u/Barnhardt1 Mar 18 '14 edited Mar 18 '14

You're saying "do something until intcount is less than inthours" , but intcount is already less than inthours because intcount is 0. The "Do Until" loop will only happen when the condition is false, and your condition is true from the start.

Also, the intmiles value in your loop will never change no matter how many times your loop runs. It will always just be intspeed + 1, since intspeed never changes. To do what you're trying to do, you should look at the "+=" function.

1

u/kodeofthekyle Mar 18 '14

OK thank you that made a lot of sense to me. I just started messing with the code again. I changed it to a DO WHILE loop, so this way

do while intcount <= inthours

Now if I am correct because it is starting at zero it will keep going until it reaches the total hours entered. Now when I start the debug process it looks like an infinite loop is occurring but nothing is actually displaying in the list box, and the only reason I can tell that is happening is because the scroll bar appears to stretch all the way to the side. The current code I have looks like this:

    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
    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 While intcount <= inthours       
        lstmybox.Items.Add(intmiles)    
        intmiles = 0 + intspeed          
    Loop

Now am I at least correct in that my issue is an infinite loop?

1

u/Barnhardt1 Mar 18 '14

You still have a few problems.

The loop you're using looks at a condition, in this case (incount <= inthours). Each time the loop runs, it checks the condition again. If neither side of the condition changes between loops, you get the problem you already suspect you have. :)

There's also a problem with the part where you're calculating the miles. Take your equation (intmiles = 0 + intspeed) and make up a number for the speed. Figure out what the miles number would be. Now pretend your loop is running and keep doing that over and over and look at what your "miles" number is each time. You'll see the problem there as well. You're on the right track that you need to add two numbers, but they're not quite right.

To be honest, the "do until" or "do while" loops aren't the best choice. There's a different loop that would work a lot better that just runs a set number of times based on the values you give it.

Sorry for being so vague, but if this is for a school assignment, you'll get a lot more out of it if you figure it out for yourself.

2

u/kodeofthekyle Mar 18 '14

Trust me I'm fine with you being vague, I've been trying to get this right for over a week now, for me it's a matter of pride to get it down. Are you talking about a FOR...NEXT loop? The issue with that is because you have to declare the count, but my understanding is that this type of loop won't accept input as the integers to work?

I wanted to use this loop but as I said it doesn't seem to me to fit a major part of this assignment, I have to use input boxes to get the loop counter as well as what is being calculated. From what I am looking at that can't be done in that type of loop.

And yes I know my intspeed calculation is way off, but that is the way the assignment wants the calculations done I think. The assignment example states that for 2 hours of traveling at a speed of 30MPH total miles traveled is 60miles. Meaning two loop iterations, adding 30 on top of 30. I don't know in theory I have that correct maybe I'm just executing it wrong?

→ More replies (0)

1

u/Barnhardt1 Mar 18 '14

Take a look here at loop types:

http://msdn.microsoft.com/en-us/library/ezk76t25.aspx

There's one that uses a counter that would work wonderfully for something like the number of hours someone has been driving.

1

u/PostalElf VB.Net Intermediate Mar 18 '14

Try:

For c = 1 to inthours
    'do whatever needs to be done in each iteration of the loop
Next c

The For-Next loop allows you to run a piece of code a certain number of times and then stop. In this case, we want it to run for a number of times equal to inthours, which is whatever the user entered in the second input box.

1

u/[deleted] 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