|
-
Sep 5th, 2005, 10:11 AM
#1
Thread Starter
Addicted Member
[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:
Option Explicit
Dim Number As Integer
Private Sub cmdOk_Click()
Static Total As Integer
Total = 0
Do
Number = txtNumber.Text
Total = Total + Number
Loop Until Total = 500
txtTotal.Text = Total
If Total > 500 then
msgbox "Total Capacity Reached"
End Sub
can you give me any pointers. i've been stuck on this for about 2.5 hours now.
thanks
-
Sep 5th, 2005, 10:23 AM
#2
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.
-
Sep 5th, 2005, 10:24 AM
#3
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.
-
Sep 5th, 2005, 10:25 AM
#4
Re: Loops making me loopy!
 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
-
Sep 5th, 2005, 10:38 AM
#5
Thread Starter
Addicted Member
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.
-
Sep 5th, 2005, 10:43 AM
#6
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.
-
Sep 5th, 2005, 10:43 AM
#7
Member
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.
-
Sep 5th, 2005, 10:44 AM
#8
Thread Starter
Addicted Member
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.
-
Sep 5th, 2005, 10:55 AM
#9
Re: Loops making me loopy!
 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:
Option Explicit
Private Sub cmdOk_Click()
Static Total As Integer
If Total >= 500 Then
MsgBox "Total Capacity Reached"
Exit Sub
ElseIf Not IsNumeric(txtNumber.Text) Then
MsgBox "Not a number"
Exit Sub
End If
Total = Total + txtNumber.Text
txtTotal.Text = Total
End Sub
Pradeep
-
Sep 5th, 2005, 10:57 AM
#10
Thread Starter
Addicted Member
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.
-
Sep 5th, 2005, 11:02 AM
#11
Re: Loops making me loopy!
VB Code:
Option Explicit
Dim Number As Integer
Private Sub cmdOk_Click()
Static Total As Integer
Total = 0
Do
Number = txtNumber.Text
Total = Total + Number
If (total mod 5) then DoEvents
Loop Until Total = 500
txtTotal.Text = Total
If Total > 500 then
msgbox "Total Capacity Reached"
End Sub
whats your error your receiving?
-
Sep 5th, 2005, 11:06 AM
#12
Re: Loops making me loopy!
 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:
Option Explicit
Private Sub cmdOk_Click()
Dim Total As Long
Dim sTemp As String
Do While Total < 500
sTemp = InputBox("Enter a number:")
If IsNumeric(sTemp) Then Total = Total + sTemp
txtTotal.Text = Total
Loop
End Sub
Pradeep
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|