|
-
Nov 22nd, 1999, 05:44 AM
#1
Thread Starter
Junior Member
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
-
Nov 22nd, 1999, 06:04 AM
#2
Hyperactive Member
[This message has been edited by compuGEEK (edited 11-22-1999).]
-
Nov 22nd, 1999, 06:06 AM
#3
Hyperactive Member
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
-
Nov 23rd, 1999, 09:16 PM
#4
Hyperactive Member
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"!
-
Nov 24th, 1999, 12:44 PM
#5
Guru
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: [email protected]
ICQ: 19552879
AIM: RYoni69
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
|