5 Replies - 1072 Views - Last Post: 20 October 2010 - 05:08 PM Rate Topic: -----

#1 Guest_Tiffany*


Reputation:

OVERFLOW error

Posted 19 October 2010 - 04:01 AM

Why am i getting an overflow error in my code...i need help.. here's the code

Dim y As Single
Dim item As String
Dim cost As Long
Dim n As Long
Dim count As Integer
Dim amount_deprec As Long
Dim total As Long
Dim sum As Long

cmdStraightline.Enabled = False
cmdDoubledecline.Enabled = False
cmdFile.Enabled = False

count = 0
y = txtPurchase.Text
item = txtDescription.Text
cost = txtCost.Text
n = txtEstimatedlife.Text

picDisplay.Print "Description: "; item
picDisplay.Print "Year of Purchase: "; y
picDisplay.Print "Cost: "; "$"; FormatNumber(cost, 2)
picDisplay.Print "Estimated Life: "; n
picDisplay.Print "Method of Depreciation: "; "Sum of Years"
picDisplay.Print
picDisplay.Print
picDisplay.Print ""; Tab(10); "Value at"; Tab(27); "Amount Deprec"; Tab(50); "Total Depreciation"
picDisplay.Print "Year"; Tab(10); "Beg of Yr"; Tab(27); "during year"; Tab(50); "to End of Year"
picDisplay.Print
 
Do
sum = n / 2 * (n + 1)
amount_deprec = cost * (n / sum)[b]******here is the problem*****[/b]
n = n - 1

total = total + amount_deprec

picDisplay.Print y; Tab(10); FormatNumber(cost, 2); Tab(27); FormatNumber(amount_deprec, 2); Tab(50); FormatNumber(total, 2)
cost = cost - amount_deprec
y = y + 1
count = count + 1
Loop Until count = n


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 19 October 2010 - 04:07 AM


Is This A Good Question/Topic? 0

Replies To: OVERFLOW error

#2 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: OVERFLOW error

Posted 19 October 2010 - 04:27 AM

Can you give some values for which you get the error? I tested for very small values and it worked.

y = ?? //also, it would be better to use CLng function y = CLng(txtPurchase.Text)
item = ??  
cost = ??
n = ??


Was This Post Helpful? 0
  • +
  • -

#3 Guest_Tiffany*


Reputation:

Re: OVERFLOW error

Posted 19 October 2010 - 01:50 PM

View PostIonut, on 19 October 2010 - 03:27 AM, said:

Can you give some values for which you get the error? I tested for very small values and it worked.

y = ?? //also, it would be better to use CLng function y = CLng(txtPurchase.Text)
item = ??  
cost = ??
n = ??





When i use 2,000 for cost, 5 for (n), and 2002 for y, it gives me overflow error...
Was This Post Helpful? 0

#4 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: OVERFLOW error

Posted 19 October 2010 - 02:29 PM

your do until condition is wrong
 count = n


you decrement n and increment count. For odd n, the condition will be never reached. Earlier i tested for 10 and that's why didn't get the error. If you need to decrement n, declare another variable and assign n's value to it.
dim n_copy as long
n_copy = n
Do
sum = n / 2 * (n + 1)
amount_deprec = cost * (n / sum)
n = n - 1

total = total + amount_deprec
cost = cost - amount_deprec
y = y + 1
count = count + 1
Loop Until count = n_copy


This post has been edited by Ionut: 19 October 2010 - 02:30 PM

Was This Post Helpful? 1
  • +
  • -

#5 Guest_Tiffany*


Reputation:

Re: OVERFLOW error

Posted 19 October 2010 - 03:14 PM

View PostIonut, on 19 October 2010 - 01:29 PM, said:

your do until condition is wrong
 count = n


you decrement n and increment count. For odd n, the condition will be never reached. Earlier i tested for 10 and that's why didn't get the error. If you need to decrement n, declare another variable and assign n's value to it.
dim n_copy as long
n_copy = n
Do
sum = n / 2 * (n + 1)
amount_deprec = cost * (n / sum)
n = n - 1

total = total + amount_deprec
cost = cost - amount_deprec
y = y + 1
count = count + 1
Loop Until count = n_copy




Thank you so so much! it worked!! you're a life saver!!:D
Was This Post Helpful? 0

#6 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,600
  • Joined: 17-April 07

Re: OVERFLOW error

Posted 20 October 2010 - 05:08 PM

good work there
but remove the count variable
change the condition like this
loop until n=0


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1