cint overflow error in my VB script
I am new to programming and taking a basic VB script programming class as a requirement for the degree I am working on. I have this program that is converting celsius numbers to farhenheit and doing it in increments and the amount to be viewed. This program works in visual logic flow chart just fine, and I have spent more than 25 hours just trying to get this program to work. I know it has to be something completely stupid, but being new to the programming world I am at a loss, and don't have the answers. Could someone look at this script, it is very basic and see if there is something I am missing. The error always comes up after the Else at fahtemp =开发者_如何转开发 (9/5) * tempaccum + 32. Any insite would be greatly appreciated. Thank you in advance for your time. Jon
Option Explicit
Dim celtemp, amttemp, increment
Dim newtemp, tempaccum, fahtemp, loopnum, templist
celtemp = inputbox("What is your starting Temp?")
amttemp = inputbox("How many temperatures do you want displayed?")
increment = inputbox("What temperature increments do you want?")
Do while loopnum < amttemp
loopnum = loopnum +1
If loopnum = 1 then
tempaccum = celtemp
fahtemp = (9/5) * (tempaccum) +32
templist = "1." & "Cel Temp: " &tempaccum & "- " & "Fah Temp: " &fahtemp
else
tempaccum = tempaccum + increment
fahtemp = (9/5) * tempaccum + 32
templist = templist &" " &loopnum & "." & "Cel Temp: " &tempaccum & "- " & "Fah Temp: " &fahtemp
End If
newtemp = celtemp + increment
Loop
Document.write "We are starting at Temp: " &celtemp
Document.write "<br> We are displaying " &amttemp & "times."
Document.write "<br> We are incrementing by: " &increment
Document.write "<br> The Temperature Table is as follows: " &templist
An overflow error occurs when you are attempting to store a value greater than the data type can handle.
If your inputs are too large, you will encounter this error.
@Jon Gammon: tempaccum = tempaccum + increment
isn't calculating as you might think it would, instead of adding the increment like it's a number, it was concatenating it like a string, i.e. if starting temperature is 10
, increment is 5
and number of times to display is 3
, you'd expect your output to be
1. Cel Temp: 10 - Fah Temp: 50
2. Cel Temp: 15 - Fah Temp: 59
3. Cel Temp: 20 - Fah Temp: 68
Instead you would get
1. Cel Temp: 10 - Fah Temp: 50
2. Cel Temp: 105 - Fah Temp: 221
3. Cel Temp: 1055 - Fah Temp: 1931
This is what caused the overflow because tempaccum
became huge and further multiplications on it would break the script. Document.Write
also isn't valid VBScript code, so I made that into a MsgBox
.
Here's a working and tested copy of your script which I rewrote a little to fix the above-mentioned issues and to improve it a little too:
Option Explicit
Dim celtemp, amttemp, increment
Dim tempaccum, fahtemp, loopnum, templist
celtemp = inputbox("What is your starting temperature?")
amttemp = inputbox("How many temperatures do you want displayed?")
increment = inputbox("What temperature increments do you want?")
' Formula to converts Celcius to Fahrenheit
Function fahrenheit(ByRef celcius)
fahrenheit = ((9 / 5)* celcius) + 32
End Function
' Some error checking
If NOT IsNumeric(amttemp) Then
amttemp = 1
Else
amttemp = Fix(amttemp) ' only interested in integer part '
End If
For loopnum = 1 To amttemp
If loopnum = 1 then
tempaccum = celtemp
fahtemp = fahrenheit(tempaccum)
templist = "1. " & "Cel Temp: " & tempaccum & _
" - " & "Fah Temp: " & fahtemp & vbCrLf
Else
tempaccum = celtemp + (increment * (loopnum - 1))
fahtemp = fahrenheit(tempaccum)
templist = templist & loopnum & ". " & _
"Cel Temp: " & tempaccum & " - " & _
"Fah Temp: " & fahtemp & vbCrLf
End If
Next
MsgBox "Starting at temperature: " & celtemp & vbCrLf & _
"Displaying " &amttemp & " times." & vbCrLf & _
"Incrementing by: " &increment & vbCrLf & vbCrLf & _
"The temperature table is as follows: " & vbCrLf & templist, 0, _
"Temperature Converter"
Update
My class is pretty basic, we haven't worked with Functions, and really the only thing we have done is inputs, while loops, Case Select and If's. SO I have had my hands full trying to code this out. Although yours works brilliantly, I am affraid it is a little more advanced than where we are at.
I see, ok, I've gone back to the original script and only updated the parts that were breaking it, as previously mentioned. Here's the new working copy:
Option Explicit
Dim celtemp, amttemp, increment
Dim tempaccum, fahtemp, loopnum, templist
celtemp = inputbox("What is your starting temperature?")
amttemp = inputbox("How many temperatures do you want displayed?")
increment = inputbox("What temperature increments do you want?")
loopnum = 1
Do While loopnum < CInt(amttemp)
If loopnum = 1 Then
tempaccum = celtemp
fahtemp = ((9 / 5) * tempaccum) + 32
templist = "1. Cel Temp: " & tempaccum & " - " & "Fah Temp: " & fahtemp & vbCrLf
Else
tempaccum = celtemp + (increment * loopnum)
fahtemp = ((9 / 5) * tempaccum) + 32
templist = templist & loopnum & ". Cel Temp: " & tempaccum & " - " & "Fah Temp: " & fahtemp & vbCrLf
End If
loopnum = loopnum + 1
Loop
MsgBox "Starting at temperature: " & celtemp & vbCrLf & _
"Displaying " & amttemp & " times." & vbCrLf & _
"Incrementing by: " & increment & vbCrLf & vbCrLf & _
"The temperature table is as follows: " & vbCrLf & templist, 0, _
"Temperature Converter"
精彩评论