Can someone explain to me what I am doing wrong? I am trying to get a word to repeat the number of times as the number input. I tried a list box and a text box and I cant seem to figure it out. I am having trouble with a program I am trying to code. 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 

Recommended Answers

All 3 Replies

Instead of using a list, use a Label.
Append your result string to the Text property of the Label.

Use of CDbl() is a cause to make an exception. You can use Val() in the place of CDbl() or both.

num = Val(txtNumber.Text)

or

num = CDbl(Val(txtNumber.Text))

What is Word(CInt(num))? Is this a sub procedure to convert an integer to a string(spelling of the integer)? If it is, quite right.

But, it arises a question about construction of your Do..Loop. As per your posting the loop should repeat the inputed number times. Here, your loop does not satify your condition. Any number you try to input it always repeat 20 times. Assign the value to txtResult inside the loop.
A little modification you have to do.

 Dim i As Double = 0
 Do While i < num
        lstResults.Items.Add(result)
        txtResult.Text &= result
        i += 1
 Loop

Suppose, it can help you.

N.B.
When your purpose is solved, please check the discussion as solved.

You could always do

Word = txtWord.Text
num = CDbl(txtNumber.Text)
txtResult.Text = String.Concat(Enumerable.Repeat(Word, num))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.