I am new to VB and working on a homework assignment that I’ve seen posted in years past. The threads were aged a year or more so I’m starting a new discussion. I am writing a program that allows me to enter sales for 5 stores and display a bar graph of asterisks (*) in a list box. Each asterisk is to present $100 in sales.

I'm 6 hours in and still having issues. Each store appears in the list box, but I am unable to get the correct number of asterisks to display. The same number of asterisk’s are displaying for each store, and the number of asterisks displayed seem to be random. I can't get it to correlate with the total sales or specific store.

I'm not sure the exact location of my error. If in the 'lstOutput' statement I replace strAsterisks with intTotalAsterisks, the correct # appears (ex. input $100 sales, result 10), but I can't seem to make 10 asterisks appear. Any advice?

My apologies, I see from previous posts that there is a way to insert line numbers with the coded, but I'm not sure how.

    Public Class Form1

        Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click

            Const intNUM_STORES As Integer = 5  'The number of stores
            Const intASTERISK As Integer = 100 'one */$100 in sales

            Dim intCount As Integer = 1     'Loop counter
            Dim intSales As Integer = 0     'To hold daily sales
            Dim decTotal As Decimal = 0     'To hold the total sales
            Dim strAsterisks As String = ""  'Asterisks
            Dim intTotalAsterisks As Integer '# Asterisks to display
            Dim strInput As String          'To hold string input
            Dim strOutput As String = String.Empty    'To hold the string output

            'Get each stores daily sales.
            Do While intCount <= intNUM_STORES
                'Get a daily sales amount from the user.
                strInput = InputBox("Enter sales for store# " & intCount.ToString(), "Store Sales")

                'Store sales
                If strInput <> String.Empty Then
                    If IsNumeric(strInput) Then
                        intSales = CInt(strInput)

                    'Calculate Asterisks
                    intTotalAsterisks = CInt(intSales / intASTERISK)

                End If

                'If sales are less than 1 set string to empty
                If intTotalAsterisks <= 0 AndAlso intSales < 1 Then
                    strAsterisks = ""
                Else
                    strAsterisks = New String(CChar("*"), intTotalAsterisks) & "*"
                End If

                'Add the daily sales to the total sales number.
                decTotal += intSales

                'Add 1 to the loop counter.
                intCount += 1

     Else
                'Display an error message for invalid input.
                MessageBox.Show("Enter a numberic value.")
                End If
            Loop

            'Clear the list box
            lstOutput.Items.Clear()

            'Add the output string to the list box
            lstOutput.Items.Add("Store #1:" & strAsterisks)
            lstOutput.Items.Add("Store #2:" & strAsterisks)
            lstOutput.Items.Add("Store #3:" & strAsterisks)
            lstOutput.Items.Add("Store #4:" & strAsterisks)
            lstOutput.Items.Add("Store #5:" & strAsterisks)

            'Display the total sales for all stores
            lblTotal.Text = decTotal.ToString("c")

        End Sub

Recommended Answers

All 8 Replies

You can do an integer divide more simply by

intTotalAsterisks = intSales \ intASTERISK

And you don't need the if-then-else for the zero case. You can just do

Dim strAsterisks As New String("*", intTotalAsterisks)

Your indentation is extremely misleading. Redo it so that it is obvious which code belongs to which if-then-else block. You are calculating strAsterisks for each store in the loop but you don't add it to the listbox until after you leave the loop so it will just use the last calculated value five times.

Something like this should work:

    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        Const intNUM_STORES As Integer = 5 'The number of stores
        Const intASTERISK As Integer = 100 'one */$100 in sales
        Dim intSales As Integer = 0 'To hold daily sales
        Dim decTotal As Decimal = 0 'To hold the total sales
        Dim strAsterisks As String = "" 'Asterisks
        Dim intTotalAsterisks As Integer '# Asterisks to display
        Dim strInput As String = "" 'To hold string input
        'Clear the list box
        lstOutput.Items.Clear()
        'Get each stores daily sales.
        For I = 1 To intNUM_STORES
            'loop the input until a number is entered
            While Not IsNumeric(strInput)
                'Get a daily sales amount from the user.
                strInput = InputBox("Enter sales for store #" & I.ToString(), "Store Sales-Numbers Only Please")
            End While
            'Store sales
            intSales = CInt(strInput)
            'Calculate Asterisks
            intTotalAsterisks = intSales / intASTERISK
            'If sales are less than 1 set string to empty
            If intTotalAsterisks <= 0 AndAlso intSales < 1 Then
                strAsterisks = ""
            Else
                strAsterisks = New String(CChar("*"), intTotalAsterisks) & "*"
            End If
            'Add the daily sales to the total sales number.
            decTotal += intSales
            'Add the output string to the list box
            lstOutput.Items.Add("Store #" & I.ToString() & ":" & strAsterisks)
            'Reset the input for the next store
            strInput = ""
        Next    
        'Display the total sales for all stores
        lblTotal.Text = decTotal.ToString("c")
    End Sub

Thank you for both (for the hints and the attempted rewrite) unfortunately, I'm still stuck. With the rewrite only "Store #1:" appears 5 times and the results are "100" followed by 11 astericks.

I understand that when using the original code, I should have the results go to the list box before leaving the loop, but I'm not clear on how. Unfortunately, my class is online, which means I don't have the face to face support. Are you able to provide any advice or rewrite suggestions? I'm really trying to learn, not just get a grade.

The code I gave you works fine. Are you changing any part of it?

I found an error in my typing that was causing the "100", but no other changes. When I run the program and enter the sales for the first store, the program executes listing all 5 lines as store #1. The other issues is that one too many asterisks appear. Ex $500 = 6 asterisks, $1000 = 11 asterisks.

Please post the new code.

Hopefully, I'm missing something basic.

Public Class Form1

    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click

        Const intNUM_STORES As Integer = 5  'The number of stores
        Const intASTERISK As Integer = 100 'one */$100 in sales
        Dim intSales As Integer = 0     'To hold daily sales
        Dim decTotal As Decimal = 0     'To hold the total sales
        Dim strAsterisks As String = ""  'Asterisks
        Dim intTotalAsterisks As Integer '# Asterisks to display
        Dim strInput As String          'To hold string input

        'Clear the list box
        lstOutput.Items.Clear()

        'Get each store's daily sales.
        For I = 1 To intNUM_STORES
            While Not IsNumeric(strInput)
                'Get daily sales amounts from user.
                strInput = InputBox("Enter sales for store# " & I.ToString(), "Store Sales (numbers only please)")
            End While

            'Store sales
            intSales = CInt(strInput)
            'Calculate Asterisks
            intTotalAsterisks = CInt(intSales / intASTERISK)

            'If sales are less than 1 set string to empty
            If intTotalAsterisks <= 0 AndAlso intSales < 100 Then
                strAsterisks = ""
            Else
                strAsterisks = New String(CChar("*"), intTotalAsterisks) & "*"
            End If

            'Add the daily sales to the total sales number.
            decTotal += intSales
            'Add the output string to the list box
            lstOutput.Items.Add("Store #" & I.ToString() & ":" & strAsterisks)
        Next
        'Display the total sales for all stores
        lblTotal.Text = decTotal.ToString("c")
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        'Close the form.
        Me.Close()
    End Sub
End Class

you missed this, strInput = "". It goes between lines 38 & 39. Also on line 11 you should have Dim strInput As String = "". You could get unexpected results the other way.

The extra asterisk comes from line 32, strAsterisks = New String(CChar("*"), intTotalAsterisks) & "*". The & "*" at the end is adding an asterisk to the end of the string.

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.