Results 1 to 2 of 2

Thread: Advice needed for half diamond using a listbox....

  1. #1
    New Member
    Join Date
    Oct 12
    Posts
    2

    Advice needed for half diamond using a listbox....

    'Get the user input.
    strUserInput = InputBox("Enter the # of astricts to build the pyramid.", "Provide a number")

    If IsNumeric(strUserInput) Then
    intNumAstricts = CInt(strUserInput)

    'Validate the user input is greater than zero.
    If intNumAstricts > 0 Then

    'Loop to increment the astrick count.
    For intCount = 0 To intNumAstricts
    strCountB += "*"
    lstPyramidBox.Items.Add(strCountB)
    Next

    'Loop to deincrement the astrick count.
    For intCount = 1 To intNumAstricts
    strCount += "*"
    lstPyramidBox.Items.Insert(intNumAstricts, strCount)
    Next

    Hi everyone. I am new to programming but I am loving it so far. One of my classes is a visual basics 2010 class. We are currently working with list boxes and loops.
    The professor has given out a asterisk half diamond program that she wants the class to do. Only three of us have come up with how to do this while displaying the half diamond
    in a list box. The above code is what I have came up with. So the first part of the code obviously increments the the asterisks until the max number that the user has input. I used 5 as a general
    number. Once the user input has been reached for the incrementation of the asterisks, the program jumps to the next loop. Here is where the problem lies. Looking at the code you would assume that
    the incrementation might continue because the strCount continues to add the "*" to what was incremented in the previous loop. There is only 2 ways that visual basic will allow the strCount variable to
    change. From above I have used the += so that the strCount should continue to add another "*" every time the loop cycles. For some reason I can only use ( = ) or ( += ) when it comes to using list
    boxes. Also notice that I am Inserting the strCount into the listbox. To insert anything into a listbox I have found that the must be an item in the index that you want insert something into. So that simply means that if the user input is 5 then you can only insert something in index 0-4. Otherwise it will not allow it.

    Now, run the program yourself and see if you can tell me why the second loop is de-incrementing when it should not be. It almost appears to be taking the user input and inserting an asterisk at twice
    what the user input is and working from the bottom up. Can this even be considered a true de-incrementation? Is it an error in the program itself?

    I hope that no one minds that this post is so long but I thought it was better to get the whole thing out in one post instead of people asking questions that are better answered now. Thanks and I look
    forward to see what explanation someone comes up with. Also, can a true de-incrementation be done with two loops only, no nested loops.

  2. #2
    New Member
    Join Date
    Oct 12
    Posts
    2

    Re: Advice needed for half diamond using a listbox....

    I did screw up . I apologize. The first For Next loop the intCount has should be = 1
    The second For Next loop the intCount should be = 2
    I had been running some other debugging test to see what the outcome would 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
  •