-
Oct 12th, 2017, 01:47 PM
#1
Thread Starter
New Member
vb asterisk shape maker
Hi, I need help getting steered in the right direction. I am trying to create a set series of shapes and am having an issue with generating a triangle. Here is my code snippet for that section.
Code:
For intRow = 0 To n
For intColumns = 0 To i Step -1
strLine = strLine & "*" ' appending a star to the string
Next
lstFigures.Items.Add(strLine) ' add current row to listBox
strLine = String.Empty ' clearing the string variable
Next
Last edited by Shaggy Hiker; Oct 12th, 2017 at 01:52 PM.
Reason: Added CODE tags.
-
Oct 12th, 2017, 01:56 PM
#2
Re: vb asterisk shape maker
Welcome to the forums. I edited your post to add [CODE][/CODE] tags. You can do this by pressing the # button and pasting the code between the tags.
I assume you are trying to make a triangle like this:
*
**
***
In which case, think about your code. Your outer loop looks reasonable, since you will need to draw a certain number of asterisks on each row. However, your inner loop will always draw i asterisks on the row, which would create a square or rectangle. What you want is to draw 1 on the first row, two on the second, three on the third, and so forth. So, how many are you drawing on each row? Not the number of columns, but the number of rows.
My usual boring signature: Nothing
-
Oct 12th, 2017, 01:57 PM
#3
Re: vb asterisk shape maker
Welcome to VBForums
It helps if you tell us what the issue is, because we generally don't run your code (we just read it to diagnose the cause of the problem you tell us about).
In this case your code was short enough that I read it to work it out, and there is a problem on the second line that means you will always get the same amount of asterisks (probably none):
Code:
For intColumns = 0 To i Step -1
Rather than the variable i, you probably meant to use the variable intRow
-
Oct 12th, 2017, 02:09 PM
#4
Thread Starter
New Member
Re: vb asterisk shape maker
Thank you si_the_geek for pointing that out. It turned out to be the exact issue I was having. Also thanks, Shaggy Hiker for fixing my last post.
-
Oct 12th, 2017, 03:27 PM
#5
Re: vb asterisk shape maker
Well, if that was the problem, how was it possible? In the code you showed, there is no variable i. The only place it showed up is in the line that Si pointed out. That should have given you a compile time error stating that i was not declared. If you have i declared elsewhere, then that would be fine. If you don't have i declared elsewhere, the only way that code would run would be with Option Explicit OFF, which would be a pretty bad thing.
My usual boring signature: Nothing
-
Oct 12th, 2017, 04:28 PM
#6
Thread Starter
New Member
Re: vb asterisk shape maker
It wasn't a compiler error as the i variable in my program is a user entered variable. The code goal I wanted was to print a triangle out of "*".
-
Oct 13th, 2017, 02:03 AM
#7
Re: vb asterisk shape maker
A variable should never be named 'i' unless it is being used as a loop counter. It is pretty much universally accepted in any programming language that 'i' is the default loop counter so it needs no explanation. Outside that, it provides no information whatsoever about its purpose and is thus a very poor choice. Whatever that variable represents is what the name should be.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|