Results 1 to 7 of 7

Thread: vb asterisk shape maker

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    4

    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.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    4

    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.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    4

    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 "*".

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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
  •  



Click Here to Expand Forum to Full Width