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