Modifing a Do...While to a For... Next Loop

I've turned everything upside down trying to get this one...

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 2045 Views - Last Post: 04 July 2008 - 09:14 AM Rate Topic: -----

#1 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Modifing a Do...While to a For... Next Loop

Post icon  Posted 03 July 2008 - 10:25 AM

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:

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



Is This A Good Question/Topic? 0
  • +

Replies To: Modifing a Do...While to a For... Next Loop

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Modifing a Do...While to a For... Next Loop

Posted 03 July 2008 - 10:34 AM

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.

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

Was This Post Helpful? 0
  • +
  • -

#3 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Modifing a Do...While to a For... Next Loop

Posted 03 July 2008 - 10:43 AM

View PostLocke37, on 3 Jul, 2008 - 01:34 PM, said:

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.

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 :crazy:

This post has been edited by LadyWolf: 03 July 2008 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Modifing a Do...While to a For... Next Loop

Posted 03 July 2008 - 10:44 AM

Moved to VB.NET :)
Was This Post Helpful? 0
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Modifing a Do...While to a For... Next Loop

Posted 03 July 2008 - 10:52 AM

...Do you absolutely HAVE to use a for loop? A While or DoWhile would be much easier...:unsure:
Was This Post Helpful? 0
  • +
  • -

#6 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Modifing a Do...While to a For... Next Loop

Posted 03 July 2008 - 11:15 AM

View PostLocke37, on 3 Jul, 2008 - 01:52 PM, said:

...Do you absolutely HAVE to use a for loop? A While or DoWhile would be much easier...:unsure:


Yes! I have to use For... Next :crazy: I agree with you that a Until or Do..While would be easier :D I think that's why I'm having difficulties with it..
Was This Post Helpful? 0
  • +
  • -

#7 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Modifing a Do...While to a For... Next Loop

Posted 03 July 2008 - 08:58 PM

Still having problems...Here's what I have now

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

Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Modifing a Do...While to a For... Next Loop

Posted 03 July 2008 - 09:21 PM

View PostLadyWolf, on 4 Jul, 2008 - 04:58 AM, said:

Still having problems...Here's what I have now

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

Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Modifing a Do...While to a For... Next Loop

Posted 03 July 2008 - 09:23 PM

You've almost got it. Lets put this in a logical sentence to see if it helps you understand the logic for this part.

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.
Was This Post Helpful? 0
  • +
  • -

#10 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Modifing a Do...While to a For... Next Loop

Posted 03 July 2008 - 09:52 PM

Now I'm getting this :crazy: "Conversion from string "" to type 'Integer' is not valid." With this 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: 03 July 2008 - 09:55 PM

Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Modifing a Do...While to a For... Next Loop

Posted 03 July 2008 - 10:01 PM

View PostLadyWolf, on 4 Jul, 2008 - 05:52 AM, said:

Now I'm getting this :crazy: "Conversion from string "" to type 'Integer' is not valid." With this 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
 
  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

Was This Post Helpful? 0
  • +
  • -

#12 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Modifing a Do...While to a For... Next Loop

Posted 03 July 2008 - 10:08 PM

Yep, and when I change inputSales as string, it says "For is not string type" :blink: lol


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

Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Modifing a Do...While to a For... Next Loop

Posted 03 July 2008 - 10:16 PM

Sorry I modified the my previous posted. :wub:
Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Modifing a Do...While to a For... Next Loop

Posted 03 July 2008 - 10:30 PM

Work around using corrected variable name
 
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

Was This Post Helpful? 0
  • +
  • -

#15 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Modifing a Do...While to a For... Next Loop

Posted 03 July 2008 - 10:34 PM

Now it's not calculating anything in the XAverageTextBox. Unless I put the code in wrong place?


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

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2