Free2Code
 
Time: 2008-11-21, 09:51am
RN Converter
Subject: RN Converter  ·  Posted: 2007-11-18, 11:04pm
Rank: ? (1)
Member #: 29679
Hi guys,

I'm fairly new to this and trying to get my head around some of the more basic things in VB!

I have written (probably the sort of "standard" beginners program), a Roman Numeral Converter.

I have gotten so far, but I keep getting an "Overflow" error. I know that it means I am trying to do too many things within the argument, but I can't get my head around what to change to make it right, help would be much appreciated! smile

Here is the sub code:


Code:
  1. Private Sub NumToRoman()
  2.     Dim strNumber As String
  3.     Dim OnDigit As Byte
  4.     Dim Digit As Byte
  5.     If Not IsNumeric(txtInput.Text) Then
  6.         MsgBox "Input not numeric!", vbOKOnly, Error
  7.         Exit Sub
  8.     End If
  9.     
  10.     If Val(txtInput) > 399999 Then
  11.         MsgBox "Number must be below 399999, sorry!", vbOKOnly, Error
  12.         Exit Sub
  13.     End If
  14.     
  15.     strNumber = Str(Int(Val(txtInput)))
  16.     lblOutput.Caption = ""
  17.     
  18.     >>>>For OnDigit = Len(strNumber) To 1 Step -1<<<< (line that's causing overflow)
  19.         Digit = Val(Mid(strNumber, OnDigit, 1))
  20.         
  21.         lblOutput.Caption = lblOutput.Caption + (ConvertToRoman(Digit, OnDigit) - 1)
  22.     Next OnDigit
  23.   
  24.     
  25. End Sub


Is it something to do with OnDigit being declared as "Byte"? Something was making me think that, but I'm a bit stuck up to that.

Thanks a lot in advance! =]

-SoLi

» Post edited 2007-11-18, 11:05pm by SoLi.

 
  Reply to this ·  Post link ·  Top
Subject: Re: RN Converter  ·  Posted: 2007-11-19, 08:51am
Rank: ? (4821)
Member #: 3416
a byte should be fine up to 128, but i think your problem here might actually be from Len(strNumber) being zero. if you start from zero and go down, you're going to hit an overflow when you try to go past -128. try setting a breakpoint on the line that's throwing the error and check the values of the relevant variables to see what's going on.

also the byte and int types in vb are actually slower than long anyway (long is 32-bit, which is native on most computers -- int and byte have to get converted), so you could try changing to long to see if that clears things up.

» Post edited 2007-11-19, 08:51am by misterhaan.

my mind is like a steel trap! it only hangs on to the big stuff. visit my forums at track7.org
 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.

icons