Click to See Complete Forum and Search --> : Help! Infinite loop!
kazooie21
Nov 22nd, 1999, 04:44 AM
The following code gives me an infinite loop. How should I change this so it won't be infinite?
Dim curSales as Currency
curSales = Val(Inputbox("Enter a sales amount")
Do While curSales > 0
Print curSales * .1
Loop
compuGEEK
Nov 22nd, 1999, 05:04 AM
[This message has been edited by compuGEEK (edited 11-22-1999).]
compuGEEK
Nov 22nd, 1999, 05:06 AM
Oops.......
Dim curSales As Currency
Dim y As Integer
curSales = Val(InputBox("enter a number:"))
For y = 1 To curSales
Print curSales * 0.1
Next y
wayneh
Nov 23rd, 1999, 08:16 PM
I don't think I'd use 'dim y as integer' since it appears that 'currency' could be a decimal number.
As well, that solution will 'for each value from 1 to cursales' - so if I enter 100 as cursales, I'll get 100 print lines.....
It depends on what your trying to achieve.
If you want to query the user until they enter '0' try this:
Dim curSales as Currency
cursales=1
Do While curSales > 0
curSales = Val(Inputbox("Enter a sales amount")
if cursales > 0 then
Print curSales * .1
else
exit do
end if
Loop
(it's a bit wordy, but it's early in the morning...)
However, if you only want to ask them once, perhaps by way of a command button or some other call to the procedure, try this:
Dim curSales as Currency
curSales = Val(Inputbox("Enter a sales amount")
if curSales > 0 then
Print curSales * .1
end if
One more thing:
You really should do your own homework. If you can't figure something out, don't post requests - go to class and learn the solutions from the teacher or the clues the teacher gives. Then come back here and see if there are additional solutions.
I've been a teacher for over 12 years and there's nothing worse than a student who doesn't do his own work and can't say "I don't know"!
Yonatan
Nov 24th, 1999, 11:44 AM
Print does not change any variable. curSales isn't affected by it, so if you entered the loop, you can't escape.
Replace this:
Print curSales * .1
With this:
curSales = curSales * .1
Print curSales
Mathematically it is still an infinite loop, but VB can't handle decimal fractions this long, so sooner or later, curSales will become zero and the loop will end.
------------------
Yonatan
Teenage Programmer
E-Mail: RZvika@netvision.net.il
ICQ: 19552879 (http://www.icq.com/19552879)
AIM: RYoni69
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.