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
Else
MessageBox.Show(Message, _
"Sales Express", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
'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")
Else
Me.xAverageLabel.Text = "0"
End If
'if sales amount can not be converted to a number
Exit For
Next sales
End Sub
End Class
Modifing a Do...While to a For... Next LoopI've turned everything upside down trying to get this one...
26 Replies - 1643 Views - Last Post: 04 July 2008 - 09:14 AM
#1
Modifing a Do...While to a For... Next Loop
Posted 03 July 2008 - 10:25 AM
Replies To: Modifing a Do...While to a For... Next Loop
#2
Re: Modifing a Do...While to a For... Next Loop
Posted 03 July 2008 - 10:34 AM
_______________________________________________________
Well...where you want to exit your for loop, the only thing you need in the If condition is this.
If IsNumeric(inputSales) Then
Exit For
End If
I'm pretty sure that's what you are trying to do.
This post has been edited by Locke37: 03 July 2008 - 10:35 AM
#3
Re: Modifing a Do...While to a For... Next Loop
Posted 03 July 2008 - 10:43 AM
Locke37, on 3 Jul, 2008 - 01:34 PM, said:
_______________________________________________________
Well...where you want to exit your for loop, the only thing you need in the If condition is this.
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: 03 July 2008 - 11:23 AM
#4
Re: Modifing a Do...While to a For... Next Loop
Posted 03 July 2008 - 10:44 AM
#5
Re: Modifing a Do...While to a For... Next Loop
Posted 03 July 2008 - 10:52 AM
#6
Re: Modifing a Do...While to a For... Next Loop
Posted 03 July 2008 - 11:15 AM
#7
Re: Modifing a Do...While to a For... Next Loop
Posted 03 July 2008 - 08:58 PM
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
'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
This post has been edited by LadyWolf: 03 July 2008 - 09:01 PM
#8
Re: Modifing a Do...While to a For... Next Loop
Posted 03 July 2008 - 09:21 PM
LadyWolf, on 4 Jul, 2008 - 04:58 AM, said:
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
'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
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: 03 July 2008 - 09:22 PM
#9
Re: Modifing a Do...While to a For... Next Loop
Posted 03 July 2008 - 09:23 PM
IF (Sales amount can be converted) THEN (Add to totol, increment sales #) ELSE (Exit For)
Put in terms of code following the logic.
If isConverted Then inputSales = salesCount + 1 salesAccum = salesAccum + sales Else Exit For End If
Hope that helps.
#10
Re: Modifing a Do...While to a For... Next Loop
Posted 03 July 2008 - 09:52 PM
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: 03 July 2008 - 09:55 PM
#11
Re: Modifing a Do...While to a For... Next Loop
Posted 03 July 2008 - 10:01 PM
LadyWolf, on 4 Jul, 2008 - 05:52 AM, said:
'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
inputSales = Integer.TryParse(InputBox(Prompt,Title,"0"), inputsales)
If inputSales .Equals(Nothing) Then exit for
This post has been edited by AdamSpeight2008: 03 July 2008 - 10:18 PM
#12
Re: Modifing a Do...While to a For... Next Loop
Posted 03 July 2008 - 10:08 PM
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: 04 July 2008 - 09:34 AM
#13
Re: Modifing a Do...While to a For... Next Loop
Posted 03 July 2008 - 10:16 PM
#14
Re: Modifing a Do...While to a For... Next Loop
Posted 03 July 2008 - 10:30 PM
Sales = Integer.TryParse(InputBox(Prompt,Title,"0"), sales)
If Sales.Equals(Nothing) Then exit for
You know you can set type of the variable of the For-loop, when you write the For-loop
For InputSales as Integer=1 to 5 ' Delete the Dim InputSales as Integer
This post has been edited by AdamSpeight2008: 03 July 2008 - 10:31 PM
#15
Re: Modifing a Do...While to a For... Next Loop
Posted 03 July 2008 - 10:34 PM
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(Integer.TryParse(InputBox(Prompt, Title, "0"), CInt(inputSales)))
If inputSales.Equals(Nothing) Then Exit For
'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
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
|
|

New Topic/Question
Reply




MultiQuote






|