Hi Guys,

I am struggling on an assignment where I have to enter an odd number to produce a triangle of stars. e.g.

If I enter 5 - then it should look something like this

*****
***
*

My code is as follows:

Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
Dim Number As Integer
Dim Index As Integer
Dim StarString(15) As String 'declare array as String


Number = CInt(txtNumber.Text)
StarString(0) = "" 'store stars in array (string)
StarString(1) = "*" 'odd numbers of stars from 1 to 15 -
StarString(2) = "" 'with negative even numbers
StarString(3) = "***"
StarString(4) = ""
StarString(5) = "*****"
StarString(6) = ""
StarString(7) = "*******"
StarString(8) = ""
StarString(9) = "*********"
StarString(10) = ""
StarString(11) = "***********"
StarString(12) = ""
StarString(13) = "*************"
StarString(14) = ""
StarString(15) = "***************"
If Number > 15 Then
MsgBox("Please enter a number between 5 and 15") 'triangle can only be formed
Else 'with a minimum of 5 stars
If Number < 5 Then 'on top row
MsgBox("Please enter a number between 5 and 15")
Else
For Index = 1 To Number Step 1
txtOutput.Text = (StarString(Index) & vbNewLine)
Next
Index = Index - 2
End If
End If

I can successfully generate the first line, but after that there is nothing.

I know it is possible to do it by using (VBNewline - number - 2), but that isn't what my tutor is looking for.

I have tried to use a loop - but can't get it to work, and so have taken it out. I know I am close - but no cigar.

Can someone please help?:confused:

Member Avatar for iamthwee

try swapping this:

StarString(3) = "***"
StarString(4) = ""
StarString(5) = "*****"
StarString(6) = ""
StarString(7) = "*******"
StarString(8) = ""
StarString(9) = "*********"
StarString(10) = ""
StarString(11) = "***********"
StarString(12) = ""
StarString(13) = "*************"
StarString(14) = ""
StarString(15) = "***************"

for a loop.

Sorry, I don't know how to do that

Member Avatar for iamthwee

Draw a flow chart then.

Your problem is here:

For Index = 1 To Number Step 1
    txtOutput.Text = (StarString(Index) & vbNewLine)
Next

You are replacing whatever you put in it.
Try one of these:

For Index = 1 To Number Step 1
    txtOutput.Text = txtOutput.Text & (StarString(Index) & vbNewLine)
Next

or

For Index = 1 To Number Step 1
    txtOutput.Text &= (StarString(Index) & vbNewLine)
Next

Your problem is here:

For Index = 1 To Number Step 1
    txtOutput.Text = (StarString(Index) & vbNewLine)
Next

You are replacing whatever you put in it.
Try one of these:

For Index = 1 To Number Step 1
    txtOutput.Text = txtOutput.Text & (StarString(Index) & vbNewLine)
Next

or

For Index = 1 To Number Step 1
    txtOutput.Text &= (StarString(Index) & vbNewLine)
Next

Thanks, I'll try that....;)

hmmm i'l just join this forum,...
to the developers,..i'l just ask how to create the stars considering the number of input numbers in an inputbox,...
we are using vb.net programming language,..

ahm we are instructed to use the nesteled for loop,..
it is a for loop inside another for loop,..

the program states that the number that will be entered in an inputbox will serve as the number of lines of the triagle,.

thank you so much for ur reply,.

Welcome @sakura_hanah07

I'm glad you got it helpful and if you have any questions please ask. You are welcome to start your own thread/New thread.

Please read the rules before posting again - http://www.daniweb.com/forums/thread78223.html and rules.

Thread Locked.

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.