Noob question about Do While and For Next Loops

mmsteelers

Member
Joined
Feb 4, 2009
Messages
9
Programming Experience
Beginner
Hi, I have to create an application that prompts the user to enter today's sales for five stores. The program should then display a simple bar graph comparing each store's sales. Create each bar in the bar graph by displaying a row of asterisks (*) in a list box. Each asterisk in a bar represents $100 in sales. I have to make two different versions of this program. One using a "do while ... loop" and the other using a "for ... next loop". Could someone give me a hint on how to do this. I the "do while ... loop" version and I have had some problems, especially with converting the numbers to asterisks. Here is a link on how the program's gui should look:
http://www.hotlinkfiles.com/files/2296675_frroi/program.jpg

Thanks to anyone in advance who can help me

VB.NET:
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        'Clear everything
        lstOutput.Items.Clear()
        ' Demonstrate the For...Next loop.
        Dim intStores As Integer ' Number of Stores
        Dim intCount As Integer    ' Loop counter
        Dim decSales As Decimal    ' To hold sales for store
        Dim strAsteriks As String    ' Holds asteriks
        Dim strInput As String     ' To get the user input
        Dim strTemp As String

        ' Store the correct starting values in the counter 
        ' and accumulator.
        intCount = 1
        intStores = 5

        ' The following loop gets the sales for each store.

        Do While intCount <= intStores
            strInput = InputBox("Enter the sales for each store " & _
                 intCount.ToString(), "Sales Amount Needed")
            If strInput <> String.Empty Then
                decSales = CDec(strInput)   ' Store input in sales
                intCount += 1              ' Increment the counter
                strTemp = "Store " & intCount.ToString _
                & ": " & decSales.ToString
                lstOutput.Items.Add(strTemp)
            End If
        Loop

    End Sub
 
Last edited by a moderator:
Displaying the output will be basically the same as getting the input. You start of with a value and you increment it, outputting an asterisk each time the value is less than a threshold. You would do this for each store, so that means a loop inside a loop.
 
still confused

I think i am getting close, but the loops inside the loop is confusing me.
I don't understand how to get strAsterisks1,strAsterisks2, strAsterisks3, strAsterisks4, or strAsterisks5. Which are the amount of asterisks I will insert at the end of strStore1, strStore2, strStore3, strStore4, and strStore5. Could someone please give me an example of how to do this in code? I am horrible at vb.net. Thanks in advance
VB.NET:
Public Class Form1

    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        'Clear everything
        lstOutput.Items.Clear()

        Dim intCount As Integer    ' Loop counter
        Dim decSales As Decimal    ' To hold sales for stores
        Dim strAsterisks1, strAsterisks2, strAsterisks3, _
        strAsterisks4, strAsterisks5 As String
        ' Holds Asterisks for each store
        Dim strStore1, strStore2, strStore3, strStore4, strStore5 As String    ' Holds Store data
        Dim strInput As String     ' To get the user input
        Dim int100 As Integer = 100  ' 100 is the amount per asterisk
        Dim intAsterisks1, intAsterisks2, intAsterisks3, _
        intAsterisks4, intAsterisks5 As Integer 

        ' Store the correct starting values in the counter 
        intCount = 1


        ' The following loop gets the sales for each store.

        Do While intCount <= 5
            strInput = InputBox("Enter the sales for each store " & _
                 intCount.ToString(), "Sales Amount Needed")
            If strInput <> String.Empty Then
                decSales = CDec(strInput)   ' Store input in sales
                intCount += 1              ' Increment the counter
            End If
        Loop

        strStore1 = "Store 1:" & strAsterisks1
        strStore2 = "Store 2:" & strAsterisks2
        strStore3 = "Store 3:" & strAsterisks3
        strStore4 = "Store 4:" & strAsterisks4
        strStore5 = "Store 5:" & strAsterisks5
        ' results are shown in lstOutput
        lstOutput.Items.Add(strStore1)
        lstOutput.Items.Add(strStore2)
        lstOutput.Items.Add(strStore3)
        lstOutput.Items.Add(strStore4)
        lstOutput.Items.Add(strStore5)
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        'Close App
        Me.Close()
    End Sub
End Class
 
Am i getting closer?

I am still having problems. Am I getting any close? Please help. Thanks in advance.

VB.NET:
 Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        'Clear everything
        lstOutput.Items.Clear()

        Dim intCount As Integer    ' Loop counter
        Dim decSales As Decimal    ' To hold sales for store
        Dim strAsterisks As String = "*" ' asterisks
        Dim strAsterisks1 As String
        Dim strAsterisks2 As String
        Dim strAsterisks3 As String
        Dim strAsterisks4 As String
        Dim strAsterisks5 As String
        ' Holds Asterisks for each store
        Dim strStore1, strStore2, strStore3, strStore4, strStore5 As String    ' Holds Store data
        Dim strInput As String     ' To get the user input
        Dim int100 As Integer = 100  ' 100 is the amount per asterisk
        Dim intAsterisks1, intAsterisks2, intAsterisks3, _
        intAsterisks4, intAsterisks5 As Integer 'Number of Asterisks to be put in strAsterisks

        ' Store the correct starting values in the counter 
        intAsterisks1 = 0
        intAsterisks2 = 0
        intAsterisks3 = 0
        intAsterisks4 = 0
        intAsterisks5 = 0

        ' The following loop gets the sales for each store.
        strStore1 = InputBox("Enter the sales for Store 1", "Sales Amount Needed", "100")
        Do While strStore1 <> String.Empty
            If intAsterisks1 <> 0 Then
                intAsterisks1 += 100            ' Increment the counter
                strAsterisks2 = CStr(lstOutput.Items.Add(strAsterisks))
            End If
            strStore2 = InputBox("Enter the sales for Store 2", "Sales Amount Needed", "100")
            Do While strStore2 <> String.Empty
                If intAsterisks2 <> 0 Then
                    intAsterisks2 += 100       ' Increment the counter
                    strAsterisks2 = CStr(lstOutput.Items.Add(strAsterisks))
                End If
            Loop

            strStore3 = InputBox("Enter the sales for Store 3", "Sales Amount Needed", "100")
            Do While strStore3 <> String.Empty
                If intAsterisks3 <> 0 Then
                    intAsterisks3 += 100             ' Increment the counter
                    strAsterisks3 = CStr(lstOutput.Items.Add(strAsterisks))
                End If
            Loop

            strStore4 = InputBox("Enter the sales for Store 4", "Sales Amount Needed", "100")
            Do While strStore4 <> String.Empty
                If intAsterisks4 <> 0 Then
                    intAsterisks4 += 100             ' Increment the counter
                    strAsterisks4 = CStr(lstOutput.Items.Add(strAsterisks))
                End If
            Loop

            strStore5 = InputBox("Enter the sales for Store 5", "Sales Amount Needed", "100")
            Do While strStore5 <> String.Empty
                If intAsterisks5 <> 0 Then
                    intAsterisks5 += 100             ' Increment the counter
                    strAsterisks5 = CStr(lstOutput.Items.Add(strAsterisks))
                End If
            Loop
        Loop
        strStore1 = "Store 1:" & strAsterisks1
        strStore2 = "Store 2:" & strAsterisks2
        strStore3 = "Store 3:" & strAsterisks3
        strStore4 = "Store 4:" & strAsterisks4
        strStore5 = "Store 5:" & strAsterisks5
        ' results are shown in lstOutput
        lstOutput.Items.Add(strStore1)
        lstOutput.Items.Add(strStore2)
        lstOutput.Items.Add(strStore3)
        lstOutput.Items.Add(strStore4)
        lstOutput.Items.Add(strStore5)

    End Sub
 
Have you learned about Arrays or Lists in your class yet?

Something like this could be greatly simplified:

VB.NET:
        Dim strAsterisks1 As String
        Dim strAsterisks2 As String
        Dim strAsterisks3 As String
        Dim strAsterisks4 As String
        Dim strAsterisks5 As String

From the example code you've posted it appears you'd like 101 to be represented with 2 asterisks. If this is incorrect you'll need to rewrite the inner loop to change this behavior.

VB.NET:
	Private Sub uxInputDoWhile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles uxInputDoWhile.Click

		'Clear ListBox Items
		Me.uxOutput.Items.Clear()

		'List to hold the asterisk representation of each store's sales
		Dim asteriskList As New List(Of String)

		Do While asteriskList.Count < 5

			'Sales for the store
			Dim salesValue As Integer = CInt(InputBox("Enter the sales for store #" & asteriskList.Count + 1, "Sales Amount Needed"))

			'Counter to determine the number of asterisks to use
			Dim asteriskCount As Integer = 0

			'Inner Do While loop suggested by jmcilhinney
			Do While salesValue > 0
				salesValue -= 100
				asteriskCount += 1
			Loop

			'Add a new string to the List consisting of the char *
			'repeated asteriskCount times
			asteriskList.Add(New String("*"c, asteriskCount))
		Loop

		'Add the items from your List to the ListBox
		Me.uxOutput.Items.AddRange(asteriskList.ToArray())

	End Sub

Another example in case you haven't learned about arrays yet:

VB.NET:
	Private Sub uxInputDoWhile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles uxInputDoWhile.Click

		'Clear ListBox Items
		Me.uxOutput.Items.Clear()

		'Counter to keep track of the number of stores that have been entered.
		Dim storeCounter As Integer = 0

		Do While storeCounter < 5

			'Sales for the store
			Dim salesValue As Integer = CInt(InputBox("Enter the sales for store #" & storeCounter + 1, "Sales Amount Needed"))

			'Counter to determine the number of asterisks to use
			Dim asteriskCount As Integer = 0

			'Inner Do While loop suggested by jmcilhinney
			Do While salesValue > 0
				salesValue -= 100
				asteriskCount += 1
			Loop

			'Add a new string to the ListBox consisting of the char *
			'repeated asteriskCount times
			Me.uxOutput.Items.Add(New String("*"c, asteriskCount))

		Loop

	End Sub
 
THANKS!! My teacher hasn't taught us how to do arrays yet. The class is a little hard in a way because it is online (and only 8 weeks), which means it is hard to get in contact with the teacher and I basically have to find out how to do things on my own, by using forums like this one and reading my textbook (which is not very descriptive). I'm sorry if my questions are very stupid at times. This is my first vb (and programming) course. Well, thanks again MattP. Your reputation should be much higher. :)
 
I'm sorry to bump such an old thread. I've spent all day trying to figure out this code with no success. Like the OP I'm taking this class online and my teacher does not return any help. I need to be studying my physics and calculus but instead my one class not pertinent to my major is destroying my study time.

I put in the code that MattP shared hoping that it would work and that I could write my own using it as a template. However I get this error when trying to use it: "invalid cast exception was unhandled" with regards to this line "Dim salesValue As Integer = CInt(InputBox("Enter the sales for store #" & storeCounter + 1, "Sales Amount Needed"))"

I owe anybody a debt of gratitude if they can help me figure this out so I can know what to or not to write in my own code so that it will end up working?
 
I'm sorry to bump such an old thread. I've spent all day trying to figure out this code with no success. Like the OP I'm taking this class online and my teacher does not return any help. I need to be studying my physics and calculus but instead my one class not pertinent to my major is destroying my study time.

I put in the code that MattP shared hoping that it would work and that I could write my own using it as a template. However I get this error when trying to use it: "invalid cast exception was unhandled" with regards to this line "Dim salesValue As Integer = CInt(InputBox("Enter the sales for store #" & storeCounter + 1, "Sales Amount Needed"))"

I owe anybody a debt of gratitude if they can help me figure this out so I can know what to or not to write in my own code so that it will end up working?
The code assumes that the value entered into the InputBox can be converted to a number. If you enter a value that isn't a number then it will fail. Either the assignment allows you to make that assumption or it doesn't. If it does then you need to honour that assumption when testing, i.e. make sure you always enter a number or just ignore those errors when they accidentally occur. If it doesn't then you need to modify the code accordingly, i.e. validate the input before using it.
 
Back
Top