Many thanks to Bernie, Matt, Rod and Robert C. for their constructive input.
Long after I went to sleep last night, Bernie arrived at two
solutions that appear to handle the logic without problem.
The first involves converting the function to integer math by
changing dollars into pennies, thus avoding the rounding problem
noted by Rod, and using while/wend loops in a manner that does not
force auto incrementing and flawed logic as noted by Robert C. and
Matt:
clear local mode
local fn MakeChange$( price as double )
dim as double dollars, quarters, dimes, nickels, pennies
dim as str255 changeStr
dim as str15 dStr, qStr, dmStr, nStr, pStr
price = int(price*100)
while price >= 100
price = price - 100 : dollars++
dStr = STR$( dollars )
wend
while price >= 25
price = price - 25 : quarters++
wend
qStr = STR$( quarters )
while price >= 10
price = price - 10 : dimes++
wend
dmStr = STR$( dimes )
while price >= 5
price = price - 5 : nickels++
wend
nStr = STR$( nickels )
while price >= 1
price = price - 1 : pennies++
wend
pStr = STR$( pennies )
changeStr = dStr + " dollars " + CHR$(13)¬
+ qStr + " quarters " + CHR$(13)¬
+ dmStr + " dimes " + CHR$(13)¬
+ nStr + " nickels " + CHR$(13)¬
+ pStr + " pennies"
end fn = changeStr
dim as double i
for i = 100 to 200 step 1
print "$"; i*.01; " changes to:"
print fn MakeChange$( i*.01 )
print
next i
Bernie's second suggestion using arrays is far more elegant:
clear local mode
local fn MakeChange$( price as long )
dim as str15 currName(5)
dim as long qty(5)
dim as short currVal(5), i
dim as str255 changeStr
currName(1) = " dollars "
currName(2) = " quarters "
currName(3) = " dimes "
currName(4) = " nickels "
currName(5) = " pennies "
currVal(1) = 100
currval(2) = 25
currVal(3) = 10
currVal(4) = 5
currVal(5) = 1
for i = 1 to 5
qty(i) = fix(price/currVal(i))
price = price - qty(i) * currVal(i)
changeStr = changeStr + str$(qty(i)) + currName(i) + chr$(13)
next i
end fn = changeStr
dim as double i
for i = 100 to 200 step 1
print "$"; i*.01; " changes to:"
print fn MakeChange$( int(i*.01*100 ))
print
next i
Now I hope Alain and Jay will come along and shorten these to 10
lines at 100x speed!
Thanks to all-- particularly Bernie-- for the help and instruction!
Ken