23

I am brand new to programming and I am running into some trouble with CInt overflow error.

Whenever the value reaches 100,000+ I get a CInt overflow error. This was a practice exercise in my intro to programming class. As far as I can see I coded it exactly how it was done in practice, but the practice shows using values as high as 300,000.

Can someone possibly explain what I might be doing wrong?

<script language="VBscript">
Option Explicit
DIM numberofshifts, totalshift1, totalshift2, _
  totalshift3, grandtotal, shiftaverage
numberofshifts=3
totalshift1 = Inputbox("How many widgets during the first shift")
totalshift2 = Inputbox("How many widgets during the second shift")
totalshift3 = Inputbox("How many widgets during the third shift")
grandtotal = cint(totalshift1) + totalshift2 + totalshift3
shiftaverage = grandtotal / numberofshifts
Document.write "The Total of the Three Shifts is " & grandtotal
Document.write "<br>The Average of the Three Shifts is " & shiftaverage
</script>
Andreas
  • 5,393
  • 9
  • 44
  • 53
Tommy Skaggs
  • 239
  • 1
  • 2
  • 3

2 Answers2

48

CInt can handle betweeen -32,768 and 32,767.

Use CLng instead of CInt.

MSDN Reference

Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
Romeo
  • 1,093
  • 11
  • 17
3

Converting string data to integers may be accomplished by using CInt() CLng() or CDbl(). It is important to remember the size limitations of these data types. Different programming languages have different limitations.
Here is a link to VBScript Data Types.

Integers can handle integers from -32,768 to 32,767. Long can handle integers from -2,147,483,648 to 2,147,483,647. Doubles can handle numbers up to 1.79769313486232E+308, (That's a bigger number than the number of atoms in the Sun, which is 1.19 octodecillion.) They are also double floating-point precision; meaning a double can also handle extremely precise decimal points.

grandtotal = cdbl(totalshift1) + totalshift2 + totalshift3 

This will eliminate the overflow problem. It won't handle the error if a user enters a non-number, but that's another topic.

Russell S
  • 105
  • 4
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – emmanuel Oct 03 '15 at 13:04
  • This answers the question completely. I wrote a script that, among other things, crawls through our intranet and calculates the RAM on domain machines. My first try was CInt. That caused an overflow. Then I found this answer from Romeo and tried CLng. That also immediately overflowed. The next data step up from long is double, so I tried CDbl and it works. It is a better answer to the question than the one given, so you are incorrect in your evaluation of my answer. – Russell S Oct 04 '15 at 20:38
  • OK. Thank you for your help. – Russell S Oct 05 '15 at 16:08
  • 1
    While there is an implicit answer to the actual question here, that's all it is - implicit. Which in turn means that you have to read between the lines to get the answer out. A much better way of phrasing this would be explicitly saying *where* the problem lies and *why* there is a problem to begin with, which the first answer does (despite me liking the fact that you added references to `CDbl()` as well) – cogumel0 Dec 07 '16 at 15:58