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:
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

    txtResult.Text = result

End Sub 
2 Years
Discussion Span
Last Post by Reverend Jim

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)


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
        txtResult.Text &= result
        i += 1

Suppose, it can help you.

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

Edited by Shark_1


You could always do

Word = txtWord.Text
num = CDbl(txtNumber.Text)
txtResult.Text = String.Concat(Enumerable.Repeat(Word, num))
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.