Results 1 to 12 of 12

Thread: [RESOLVED] Loops making me loopy!

  1. #1

    Thread Starter
    Addicted Member oldmcgroin's Avatar
    Join Date
    Jul 2005
    Location
    Manchester, UK
    Posts
    182

    Resolved [RESOLVED] Loops making me loopy!

    hey guys, i'm stuck again!

    I'm learning about loops.

    The exercise i'm doing wants me to enter numbers into a box (it doesn't specify what sort so i'm using a text box). It wants me to show the running total of the numbers entered into the box (this is shown in another text box box) and it wants me to keep entering numbers until the running total is 500. If 500 is exceeded then a msg box pops up.

    Here's my code so far:
    VB Code:
    1. Option Explicit
    2. Dim Number As Integer
    3.  
    4. Private Sub cmdOk_Click()
    5. Static Total As Integer
    6.  
    7.  
    8. Total = 0
    9.  
    10. Do
    11. Number = txtNumber.Text
    12.  
    13. Total = Total + Number
    14.  
    15.  
    16. Loop Until Total = 500
    17.  
    18. txtTotal.Text = Total
    19.  
    20. If Total > 500 then
    21.  
    22. msgbox "Total Capacity Reached"
    23.  
    24. End Sub

    can you give me any pointers. i've been stuck on this for about 2.5 hours now.

    thanks

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Loops making me loopy!

    It cant exceed it because you have 'Loop Until Total = 500' ...as soon as it gets to 500 it will exit. You should use:

    If Total >= 500 then

    instead of

    If Total > 500 then

    if you want to signal that the end is reached.

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Loops making me loopy!

    This might work better...

    Code:
    Option Explicit
    Dim Number As Integer
    
    Private Sub cmdOk_Click()
    Static Total As Integer
    
    Total = 0
    Number = txtNumber.Text ' no reason to have this in the loop
    ' it only needs to be evaluated once
    
    Debug.Print "Number="; Number
    
    Do
       Total = Total + Number
       Debug.Print "Total="; Total
    Loop Until Total >= 500 ' >= is required as it will sometimes stop at 500
    ' and sometimes exceed 500
    
    Debug.Print "Out of Loop, Total="; Total
    
    txtTotal.Text = Total
    
    If Total > 500 then
    msgbox "Total Capacity Reached"
    
    End Sub
    By using DEBUG.PRINT statements you will see the code execute and have a better idea what's going on and what might be wrong.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Loops making me loopy!

    Quote Originally Posted by baja_yu
    It cant exceed it because you have 'Loop Until Total = 500' ...as soon as it gets to 500 it will exit. You should use:

    If Total >= 500 then

    instead of

    If Total > 500 then

    if you want to signal that the end is reached.
    Well - you beat me by just a second or so - but I was typing a much longer response

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Addicted Member oldmcgroin's Avatar
    Join Date
    Jul 2005
    Location
    Manchester, UK
    Posts
    182

    Re: Loops making me loopy!

    i'm getting wierd results doing it like this. for starters it automatically sets the total as 500. if i put in a number 5 it sets the total as 500, if i put in 6 then it sets the total as 504, putting in 3 sets its as 502. very strange.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Loops making me loopy!

    If you put in the DEBUG.PRINT statements you can "step" through the code one line at a time (with F8) and watch exactly what is going on.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    Member
    Join Date
    Jun 2005
    Posts
    51

    Re: Loops making me loopy!

    That's because 6 and 3 don't go evenly into 500

    EDIT: I found another problem. At the bottom of your code:

    This
    txtTotal.Text = Total

    Should be this
    Total = txtTotal.Text
    Last edited by millertime; Sep 5th, 2005 at 10:46 AM.

  8. #8

    Thread Starter
    Addicted Member oldmcgroin's Avatar
    Join Date
    Jul 2005
    Location
    Manchester, UK
    Posts
    182

    Re: Loops making me loopy!

    i know what its doing now. its just looping any number i put in until the total is over 500 when it stops.

    well i'm a bit confused about the question now. i can't see how i can do this question using loops.

  9. #9
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Loops making me loopy!

    Quote Originally Posted by oldmcgroin
    i'm getting wierd results doing it like this. for starters it automatically sets the total as 500. if i put in a number 5 it sets the total as 500, if i put in 6 then it sets the total as 504, putting in 3 sets its as 502. very strange.
    you are doing :
    Code:
    Static Total As Integer
    Total = 0
    This way Total will never be increasing.

    This one should work:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub cmdOk_Click()
    4.     Static Total As Integer
    5.     If Total >= 500 Then
    6.         MsgBox "Total Capacity Reached"
    7.         Exit Sub
    8.     ElseIf Not IsNumeric(txtNumber.Text) Then
    9.         MsgBox "Not a number"
    10.         Exit Sub
    11.     End If
    12.     Total = Total + txtNumber.Text
    13.     txtTotal.Text = Total
    14. End Sub

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  10. #10

    Thread Starter
    Addicted Member oldmcgroin's Avatar
    Join Date
    Jul 2005
    Location
    Manchester, UK
    Posts
    182

    Re: Loops making me loopy!

    Ok after a ray of light i've almost got it.

    I'm using an inputbox now. However i can't see the running total until i reach 500 and whenever i close the input box i get an error. can you give me some code to get round this.

  11. #11
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Loops making me loopy!

    VB Code:
    1. Option Explicit
    2. Dim Number As Integer
    3.  
    4. Private Sub cmdOk_Click()
    5. Static Total As Integer
    6.  
    7.  
    8. Total = 0
    9.  
    10. Do
    11. Number = txtNumber.Text
    12.  
    13. Total = Total + Number
    14.  
    15. If (total mod 5) then DoEvents
    16.  
    17. Loop Until Total = 500
    18.  
    19. txtTotal.Text = Total
    20.  
    21. If Total > 500 then
    22.  
    23. msgbox "Total Capacity Reached"
    24.  
    25. End Sub
    whats your error your receiving?

  12. #12
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Loops making me loopy!

    Quote Originally Posted by oldmcgroin
    Ok after a ray of light i've almost got it.

    I'm using an inputbox now. However i can't see the running total until i reach 500 and whenever i close the input box i get an error. can you give me some code to get round this.
    OK for Inputbox it goes like this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub cmdOk_Click()
    4.     Dim Total As Long
    5.     Dim sTemp As String
    6.    
    7.     Do While Total < 500
    8.         sTemp = InputBox("Enter a number:")
    9.         If IsNumeric(sTemp) Then Total = Total + sTemp
    10.         txtTotal.Text = Total
    11.     Loop
    12. End Sub

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width