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>
CInt
can handle betweeen -32,768 and 32,767.
Use CLng
instead of CInt
.
MSDN Reference
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With