Antwinette Jones
Antwinette Jones

Reputation: 13

how to use a loop to repeat a word in vb.net

I am having trouble with a program I am trying to code. I have tried to use a list box and a text box but I do not get the word to repeat it just appears once. I need to know what I am doing wrong. These are the instructions and I am trying to figure it out.

Write a program that requests the user to enter a positive integer between 1 and 20 and a word. The program will first use a loop to validate the integer input (refer to example 2 in powerpoint as an example) and then use another loop to display the word the same number of times as the integer input. For example, if the user enters 5 and “hello”, the result will show the following: hellohellohellohellohello Hint: in the second loop, append the word to the result in each iteration of the loop. For example, if the input is 5, the loop will run 5 times and in each iteration of the loop, the word is repeated once. So if the loop runs 5 times, the word is repeated 5 times.

This is what I have so far:

    Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
    Dim num As Double = 0
    Dim Word As String = ""
    Dim result As String

    Word = txtWord.Text
    num = CDbl(txtNumber.Text)
    result = Word(CInt(num))


    Do While num <= 20
        lstResults.Items.Add(result)
    Loop

    txtResult.Text = result

End Sub 

Upvotes: 0

Views: 4384

Answers (1)

furkle
furkle

Reputation: 5059

The first problem here is that you're not actually using num in any meaningful sense. num is presumably the word you're taking from a TextBox, but you're not incrementing it at all. As it stands, your While loop is either never going to end if the user enters a number less than 20, or never execute at all if the user enters a number greater than 20.

What you should be doing is constructing your Do While loop like so:

For i As Integer = 0 To Num-1
    // do something here
Next

This way, the loop will execute exactly num times. Notice that a loop is 0-based. So if you want to make 5 loops you have to set the limits to '0 to 4'.

The second problem is that you're not actually using the contents of lstResults at all. If all you're trying to do is duplicate a string Num times, the easy (somewhat non-performant) way of doing that would be to just concatenate a string:

Dim str As String = ""
For i As Integer = 0 To Num-1
    str += Word
Next

txtResult.Text = str

Concatenating strings requires the creation of a new string every time, so if performance is a concern at all, you should use StringBuilder instead of String, then call ToString() on the StringBuilder after you finish building it.

I'm not really sure at all what you're trying to with the result definition (result = Word(CInt(num))), but whatever it is it's not going to work. Word is a String object, not a function, and as such you can't call Word() to accomplish anything. Even if you could, why would you

Lastly, you shouldn't be casting num to a CDbl - you shouldn't be using Double as a check value for a For or While loop. What would it even mean to perform an action, for example, 6.37 times? And if you you're just using it to check whether your counter isn't greater than that value, why bother using a double if the counter is an Integer? (It's also relevant to mention that you should only ever use Integers for your counter value - floating point arithmetic is too unpredictable for those purposes.)

Upvotes: 1

Related Questions