Join 136,479 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,680 people online right now. Registration is fast and FREE... Join Now!
User needs to put in 5 sales amounts before an Average is calculated, and if the sales amount cannot be converted to a number use Exit For statement. Not sure if I'm thinking along the right lines or not. Here's what I have at the moment:
CODE
Option Explicit On Option Strict On
Public Class MainForm
Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click Me.Close() End Sub
Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click 'calculates and displays the average sales amount
Const Prompt As String = _ "Enter a sales amount. Click Cancel to end." Const Title As String = "Sales Entry" Const Message As String = _ "Please re-enter the sales amount." Dim inputSales As String Dim sales As Decimal Dim salesCount As Integer Dim salesAccum As Decimal Dim average As Decimal Dim isConverted As Boolean
inputSales = InputBox(Prompt, Title, "0")
'repeat as long as the user enters 5 sales amount For sales = 1 To 5
'try to convert the sales amount to a number isConverted = Decimal.TryParse(inputSales, sales)
'if the sales amount can be converted to a 'number, update the counter and accumulator; 'otherwise, display a message
If isConverted Then salesCount = salesCount + 1 salesAccum = salesAccum + sales
'if counter is greater than 4, calculate 'and display the average only if the user 'has entered 5 valid sales amounts; otherwise 'display 0 as the average sales amount
If salesCount > 4 Then average = _ salesAccum * Convert.ToDecimal(salesCount) Me.xAverageLabel.Text = average.ToString("C2")
To any moderator that reads this thread, it is a VB.NET question. _______________________________________________________
Well...where you want to exit your for loop, the only thing you need in the If condition is this.
vb
If IsNumeric(inputSales) Then Exit For End If
I'm pretty sure that's what you are trying to do.
oops Wasn't aware I was posting in the wrong place. "If sales amount cannot be converted to a number" How could this IF statement work? inputSales = InPutBox
This post has been edited by LadyWolf: 3 Jul, 2008 - 10:23 AM
Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click Me.Close() End Sub
Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click 'calculates and displays the average sales amount
Const Prompt As String = _ "Enter sales amounts. Click Cancel to end." Const Title As String = "Sales Entry" Dim inputSales As Integer Dim sales As Decimal Dim salesCount As Integer Dim salesAccum As Decimal Dim average As Decimal Dim isConverted As Boolean
Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click Me.Close() End Sub
Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click 'calculates and displays the average sales amount
Const Prompt As String = _ "Enter sales amounts. Click Cancel to end." Const Title As String = "Sales Entry" Dim inputSales As Integer Dim sales As Decimal Dim salesCount As Integer Dim salesAccum As Decimal Dim average As Decimal Dim isConverted As Boolean 'get 5 sales amounts inputSales = CInt(InputBox(Prompt, Title, "0"))
'repeat as long as the user enters a sales amount For inputSales = 1 To 5
'try to convert the sales amount to a number isConverted = Decimal.TryParse(CStr(inputSales), sales)
'if the sales amount can be converted to a 'number, update the counter and accumulator; 'otherwise, display a message
If isConverted Then inputSales = salesCount + 1 salesAccum = salesAccum + sales
End If
'if number cannot be converted then(and have exit for here?) Next inputSales
'if counter is greater than 4, calculate 'and display the average sales amount; otherwise 'display 0 as the average sales amount
If salesCount > 4 Then average = _ salesAccum / Convert.ToDecimal(salesCount) Me.xAverageLabel.Text = average.ToString("C2")
Else
Me.xAverageLabel.Text = "0" End If End Sub End Class
shouldn't that be
CODE
Option Explicit On Option Strict On
Public Class MainForm
Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click Me.Close() End Sub
Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click 'calculates and displays the average sales amount
Const Prompt As String = _ "Enter sales amounts. Click Cancel to end." Const Title As String = "Sales Entry" Dim inputSales As Integer Dim sales As Decimal Dim salesCount As Integer Dim salesAccum As Decimal Dim average As Decimal Dim isConverted As Boolean
'repeat as long as the user enters a sales amount For inputSales = 1 To 5 'get 5 sales amounts inputSales = CInt(InputBox(Prompt, Title, "0"))
'try to convert the sales amount to a number isConverted = Decimal.TryParse(CStr(inputSales), sales)
'if the sales amount can be converted to a 'number, update the counter and accumulator; 'otherwise, display a message
If isConverted Then inputSales = salesCount + 1 salesAccum = salesAccum + sales
End If
'if number cannot be converted then(and have exit for here?) Next inputSales
'if counter is greater than 4, calculate 'and display the average sales amount; otherwise 'display 0 as the average sales amount
If salesCount > 4 Then average = _ salesAccum / Convert.ToDecimal(salesCount) Me.xAverageLabel.Text = average.ToString("C2")
Else
Me.xAverageLabel.Text = "0" End If End Sub End Class
This post has been edited by AdamSpeight2008: 3 Jul, 2008 - 08:22 PM
Now I'm getting this "Conversion from string "" to type 'Integer' is not valid." With this code:
CODE
Option Explicit On Option Strict On
Public Class MainForm
Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click Me.Close() End Sub
Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click 'calculates and displays the average sales amount
Const Prompt As String = _ "Enter sales amounts. Click Cancel to end." Const Title As String = "Sales Entry" Dim inputSales As Integer Dim sales As Decimal Dim salesCount As Integer Dim salesAccum As Decimal Dim average As Decimal Dim isConverted As Boolean
'repeat as long as the user enters a sales amount For inputSales = 1 To 5 'get 5 sales amounts inputSales = CInt(InputBox(Prompt, Title, "0"))<---This is where error is..
'try to convert the sales amount to a number isConverted = Decimal.TryParse(CStr(inputSales), sales)
'if the sales amount can be converted to a 'number, update the counter and accumulator; 'otherwise, display a message
If isConverted Then inputSales = CInt(CStr(salesCount + 1)) salesAccum = salesAccum + sales Else Exit For End If
Next inputSales
'if counter is greater than 4, calculate 'and display the average sales amount; otherwise 'display 0 as the average sales amount
If salesCount > 4 Then average = _ salesAccum / Convert.ToDecimal(salesCount) Me.xAverageLabel.Text = average.ToString("C2")
Else
Me.xAverageLabel.Text = "0" End If End Sub End Class
mmmmmmmmmmmmmmmmmmmmmmm
This post has been edited by LadyWolf: 3 Jul, 2008 - 08:55 PM
Now I'm getting this "Conversion from string "" to type 'Integer' is not valid." With this code:
CODE
'repeat as long as the user enters a sales amount For inputSales = 1 To 5 'get 5 sales amounts inputSales = CInt(InputBox(Prompt, Title, "0"))<---This is where error is..
If the inputbox's input is contains non-numerical characters then, the CInt will failed. Inputbox outputs strings.
Work around
vb
inputSales = Integer.TryParse(InputBox(Prompt,Title,"0"), inputsales) If inputSales .Equals(Nothing) Then exit for
This post has been edited by AdamSpeight2008: 3 Jul, 2008 - 09:18 PM
Yep, and when I change inputSales as string, it says "For is not string type" lol
CODE
Option Explicit On Option Strict On
Public Class MainForm
Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click Me.Close() End Sub
Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click 'calculates and displays the average sales amount
Const Prompt As String = _ "Enter sales amounts. Click Cancel to end." Const Title As String = "Sales Entry" Dim inputSales As String Dim sales As Decimal Dim salesCount As Integer Dim salesAccum As Decimal Dim average As Decimal Dim isConverted As Boolean
'repeat as long as the user enters a sales amount For inputSales = 1 To 5<---Where error is 'get 5 sales amounts inputSales = InputBox(Prompt, Title, "0")
'try to convert the sales amount to a number isConverted = Decimal.TryParse(CStr(inputSales), sales)
'if the sales amount can be converted to a 'number, update the counter and accumulator; 'otherwise, display a message
If isConverted Then inputSales = CStr(salesCount + 1) salesAccum = salesAccum + sales Else Exit For End If
Next inputSales
'if counter is greater than 4, calculate 'and display the average sales amount; otherwise 'display 0 as the average sales amount
If salesCount > 4 Then average = _ salesAccum / Convert.ToDecimal(salesCount) Me.xAverageLabel.Text = average.ToString("C2")
Else
Me.xAverageLabel.Text = "0" End If End Sub End Class
This post has been edited by jayman9: 4 Jul, 2008 - 08:34 AM