Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

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!




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

3 Pages V  1 2 3 >  
Reply to this topicStart new topic

Modifing a Do...While to a For... Next Loop, I've turned everything upside down trying to get this one...

LadyWolf
3 Jul, 2008 - 09:25 AM
Post #1

D.I.C Head
**

Joined: 25 Jun, 2008
Posts: 168


My Contributions
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

            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

User is offlineProfile CardPM
+Quote Post

Locke37
RE: Modifing A Do...While To A For... Next Loop
3 Jul, 2008 - 09:34 AM
Post #2

I'm not a thief...I prefer the term TREASURE HUNTER!
Group Icon

Joined: 20 Mar, 2008
Posts: 1,013



Thanked: 39 times
Dream Kudos: 325
My Contributions
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.

This post has been edited by Locke37: 3 Jul, 2008 - 09:35 AM
User is online!Profile CardPM
+Quote Post

LadyWolf
RE: Modifing A Do...While To A For... Next Loop
3 Jul, 2008 - 09:43 AM
Post #3

D.I.C Head
**

Joined: 25 Jun, 2008
Posts: 168


My Contributions
QUOTE(Locke37 @ 3 Jul, 2008 - 01:34 PM) *

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 crazy.gif

This post has been edited by LadyWolf: 3 Jul, 2008 - 10:23 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Modifing A Do...While To A For... Next Loop
3 Jul, 2008 - 09:44 AM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,993



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Moved to VB.NET smile.gif
User is offlineProfile CardPM
+Quote Post

Locke37
RE: Modifing A Do...While To A For... Next Loop
3 Jul, 2008 - 09:52 AM
Post #5

I'm not a thief...I prefer the term TREASURE HUNTER!
Group Icon

Joined: 20 Mar, 2008
Posts: 1,013



Thanked: 39 times
Dream Kudos: 325
My Contributions
...Do you absolutely HAVE to use a for loop? A While or DoWhile would be much easier...unsure.gif
User is online!Profile CardPM
+Quote Post

LadyWolf
RE: Modifing A Do...While To A For... Next Loop
3 Jul, 2008 - 10:15 AM
Post #6

D.I.C Head
**

Joined: 25 Jun, 2008
Posts: 168


My Contributions
QUOTE(Locke37 @ 3 Jul, 2008 - 01:52 PM) *

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


Yes! I have to use For... Next crazy.gif I agree with you that a Until or Do..While would be easier biggrin.gif I think that's why I'm having difficulties with it..
User is offlineProfile CardPM
+Quote Post

LadyWolf
RE: Modifing A Do...While To A For... Next Loop
3 Jul, 2008 - 07:58 PM
Post #7

D.I.C Head
**

Joined: 25 Jun, 2008
Posts: 168


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

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

        '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: 3 Jul, 2008 - 08:01 PM
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: Modifing A Do...While To A For... Next Loop
3 Jul, 2008 - 08:21 PM
Post #8

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 799



Thanked: 51 times
Dream Kudos: 2175
My Contributions
QUOTE(LadyWolf @ 4 Jul, 2008 - 04:58 AM) *

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

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
'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
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Modifing A Do...While To A For... Next Loop
3 Jul, 2008 - 08:23 PM
Post #9

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,944



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
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.
CODE


            If isConverted Then
                inputSales = salesCount + 1
                salesAccum = salesAccum + sales
             Else
                Exit For
             End If


Hope that helps.
User is online!Profile CardPM
+Quote Post

LadyWolf
RE: Modifing A Do...While To A For... Next Loop
3 Jul, 2008 - 08:52 PM
Post #10

D.I.C Head
**

Joined: 25 Jun, 2008
Posts: 168


My Contributions
Now I'm getting this crazy.gif "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
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: Modifing A Do...While To A For... Next Loop
3 Jul, 2008 - 09:01 PM
Post #11

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 799



Thanked: 51 times
Dream Kudos: 2175
My Contributions
QUOTE(LadyWolf @ 4 Jul, 2008 - 05:52 AM) *

Now I'm getting this crazy.gif "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
User is offlineProfile CardPM
+Quote Post

LadyWolf
RE: Modifing A Do...While To A For... Next Loop
3 Jul, 2008 - 09:08 PM
Post #12

D.I.C Head
**

Joined: 25 Jun, 2008
Posts: 168


My Contributions
Yep, and when I change inputSales as string, it says "For is not string type" blink.gif 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
User is offlineProfile CardPM
+Quote Post

3 Pages V  1 2 3 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 06:26PM