Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old November 5th, 2003, 05:41 AM
Locor Locor is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Gibraltar
Posts: 22 Locor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m
Reputation Power: 0
Send a message via AIM to Locor Send a message via Yahoo to Locor
Unhappy Dispensing stamps...works only for some values, I cant see the pattern.

This code is meant to decide what post stamps to give a person for any given amount...can someone tell my why it works for 0.53 and 1.53 but not for 2.53...

Dim stampsale 'the amount
Dim stamp(18) 'array with stamp prices
Dim stamp_text(18) 'the amount of stamps of any given type

For i = 0 To 17
stamp_text(i) = 0
Next

stamp(0) = 5
stamp(1) = 3
stamp(2) = 2
stamp(3) = 1
stamp(4) = 0.66
stamp(5) = 0.54
stamp(6) = 0.5
stamp(7) = 0.42
stamp(8) = 0.4
stamp(9) = 0.3
stamp(10) = 0.28
stamp(11) = 0.2
stamp(12) = 0.12
stamp(13) = 0.1
stamp(14) = 0.05
stamp(15) = 0.04
stamp(16) = 0.02
stamp(17) = 0.01



stampsale = Val(Text3.Text)

i = 0

Do While i < 18

Do While stampsale >= stamp(i)

stampsale = stampsale - stamp(i)
stamp_text(i) = stamp_text(i) + 1

Loop

i = i + 1

Loop

Reply With Quote
  #2  
Old November 5th, 2003, 08:17 AM
Locor Locor is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Gibraltar
Posts: 22 Locor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m
Reputation Power: 0
Send a message via AIM to Locor Send a message via Yahoo to Locor
doh. it was just the system rounding up, i've redone it all using integers and dividing by 100 at the end and its fine.

Reply With Quote
  #3  
Old November 5th, 2003, 08:37 AM
Fisherman's Avatar
Fisherman Fisherman is offline
Inherits Programmer.Slacker
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2003
Location: Between my Id and your Ego
Posts: 2,171 Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 9 h 1 m 37 sec
Reputation Power: 110
Send a message via ICQ to Fisherman Send a message via AIM to Fisherman
There is nothing inherently wrong with your code. The problem is in the way that Visual Basic is handling your variables. The problem is in rounding errors. On that last iteration of the code with the value of the text box = 1.53, the resultant value of the loop is .01. However, on the last iteration of the loop with the value of 2.53, the value is .0099999999999, which is evaluated as less than the value of the array. I fixed the problem by changing the array declaration to currency, but, as it looks like you're working in VBScript, you wouldn't be able to do that.... You might try using the CCur() function in any method that deals with assignment or evaluation. like this
Code:
Private Sub Command1_Click()
Dim stampsale  'the amount
Dim stamp(18)  'array with stamp prices
Dim stamp_text(18) 'the amount of stamps of any given type
Dim i As Integer

For i = 0 To 17
stamp_text(i) = 0
Next i

stamp(0) = ccur(5)
stamp(1) = ccur(3)
stamp(2) = ccur(2)
stamp(3) = ccur(1)
stamp(4) = ccur(0.66)
stamp(5) = ccur(0.54)
stamp(6) = ccur(0.5)
stamp(7) = ccur(0.42)
stamp(8) = ccur(0.4)
stamp(9) = ccur(0.3)
stamp(10) =  ccur(0.28)
stamp(11) =  ccur(0.2)
stamp(12) =  ccur(0.12)
stamp(13) =  ccur(0.1)
stamp(14) =  ccur(0.05)
stamp(15) =  ccur(0.04)
stamp(16) =  ccur(0.02)
stamp(17) =  ccur(0.01)



stampsale = ccur(Val(Text3.Text))

i = 0

Do While i < 18

    Do While stampsale >= stamp(i)

        stampsale = stampsale - stamp(i)
        stamp_text(i) = stamp_text(i) + 1

    Loop

    i = i + 1

Loop
MsgBox "Stamps: " & Join(stamp_text, ", ")
End Sub


That worked in VB6, but it may not in VBScript... you could also potentially use the formatcurrency() function instead of CCur. Also, rounding your values could handle part of it...Visual basic has a function called rnd() that could work, but it may not provide all of the functionality you need. You would want to use it here
Code:
Do While i < 18

    Do While RND(stampsale) >= stamp(i)

        stampsale = stampsale - stamp(i)
        stamp_text(i) = stamp_text(i) + 1

    Loop

    i = i + 1

Loop


if it doesn't provide all of the functionality you need, then you can find a custom round function from Microsoft here
__________________
Fisherman

"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - A.Einstein

Reply With Quote
  #4  
Old November 5th, 2003, 08:38 AM
Fisherman's Avatar
Fisherman Fisherman is offline
Inherits Programmer.Slacker
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2003
Location: Between my Id and your Ego
Posts: 2,171 Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 9 h 1 m 37 sec
Reputation Power: 110
Send a message via ICQ to Fisherman Send a message via AIM to Fisherman
OK.. nevermind ... here I am typing happily away, and you've already fixed it!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Dispensing stamps...works only for some values, I cant see the pattern.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway