none
Need Help - Visual Basic Roman Numeral Converter not Working Properly RRS feed

  • Question

  • I have two issues with this code. First of all, when you erase the value that was converted, and input another number, it keeps the value that was used before and reputes it in with the newly converted number such as:

    4000 = MMMM

    I = MMMMI (Supposed to only be I)

    (It keeps the value that was converted and puts it in front of the next)

    My Next issues is it sometimes dosent include the conversion for the "V"s and the Xs and Ls, only when there not alone such as:

    5 = V

    1345 = MCCCIV (It leaves out the conversion for 40)

    Public Class frmRomain
        Dim Input As Integer = 0

        Dim M As Integer = 0
        Dim MFoward As Integer = 0
        Dim MRomain As String

        Dim D As Integer = 0
        Dim DFoward As Integer = 0
        Dim DCromain As String

        Dim C As Integer = 0
        Dim CFoward As Integer = 0

        Dim L As Integer = 0
        Dim LFoward As Integer = 0
        Dim LXromain As String

        Dim X As Integer = 0
        Dim XFoward As Integer = 0

        Dim V As Integer = 0
        Dim VFoward As Integer = 0
        Dim VIroman As String

        Dim I As Integer = 0



        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
            Input = txtNumbre.Text

            M = Input \ 1000
            MFoward = ((Input / 1000) - (Input \ 1000)) * 1000

            D = MFoward \ 500
            DFoward = ((Input / 500) - (Input \ 500)) * 500

            C = DFoward \ 100
            CFoward = ((Input / 100) - (Input \ 100)) * 100

            L = CFoward \ 50
            LFoward = ((Input / 50) - (Input \ 500)) * 50

            X = LFoward \ 10
            XFoward = ((Input / 10) - (Input \ 500)) * 10

            V = XFoward \ 5
            VFoward = ((Input / 5) - (Input \ 5)) * 5

            I = VFoward

            If (M = 1) Then
                MRomain = "M"
            End If

            If (C = 1) And (D = 0) Then
                DCromain = "C"
            ElseIf (C = 2) And (D = 0) Then
                DCromain = "CC"
            ElseIf (C = 3) And (D = 0) Then
                DCromain = "CCC"
            ElseIf (C = 4) And (D = 0) Then
                DCromain = "CD"
           ElseIf (D = 1) And (C = 0) Then
                DCromain = "D"
            ElseIf (D = 1) And (C = 1) Then
                DCromain = "DC"
            ElseIf (D = 1) And (C = 2) Then
                DCromain = "DCC"
            ElseIf (D = 1) And (C = 3) Then
                DCromain = "DCCC"
            ElseIf (D = 1) And (C = 4) Then
                DCromain = "CM"
            End If

            If (X = 1) And (L = 1) Then
                LXromain = "LX"
            ElseIf (X = 2) And (L = 1) Then
                LXromain = "LXX"
            ElseIf (X = 3) And (L = 1) Then
                LXromain = "LXXX"
            ElseIf (X = 4) And (L = 1) Then
                LXromain = "XC"
            ElseIf (L = 1) And (X = 0) Then
                LXromain = "L"

            ElseIf (X = 1) And (L = 0) Then
                LXromain = "X"
            ElseIf (X = 2) And (L = 0) Then
                LXromain = "XX"
            ElseIf (X = 3) And (L = 0) Then
                LXromain = "XXX"
            ElseIf (X = 4) And (L = 0) Then
                LXromain = "XL"
            End If


            If (I = 4) And (V = 1) Then
                VIromain = "IX"
            ElseIf (I = 3) And (V = 1) Then
                VIromain = "VIII"
            ElseIf (I = 2) And (V = 1) Then
                VIromain = "VII"
            ElseIf (I = 1) And (V = 1) Then
                VIromain = "VI"
            ElseIf (I = 4) Then
                VIromain = "IV"
            ElseIf (V = 1) Then
                VIromain = "V"
            ElseIf (I = 1) Then
                VIromain = "I"
            ElseIf (I = 2) Then
                VIromain = "II"
            ElseIf (I = 3) Then
                VIromain = "III"
            ElseIf (I = 4) Then
            End If

            txtNumeral.Text = MRomain & DCromain & LXromain & VIromain


        End Sub

        Private Sub btnErase_Click(sender As Object, e As EventArgs) Handles btnEraseClick

            Me.txtNumbre.Text = "0"
            Me.txtNumeral.Text = "0"
        End Sub
    End Class

    Friday, October 26, 2018 5:34 PM

All replies

  • Visual Basic - Visual Studio

    Sunday, October 28, 2018 12:45 AM
  • Hi ChristianTellier,

    Welcome to the MSDN forum.

    Refer to the description, your issue is about the VB.NET development and our forum is to discuss the VS IDE, I will help you move this thread to the appropriate forum for a better support, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Monday, October 29, 2018 3:19 AM
  • Hi ,

    try my code

    Public Class Form1
        Public Shared Function GetLoumaNumber(ByVal n As Integer) As String
            Dim arabic As Integer() = New Integer(12) {}
            Dim roman As String() = New String(12) {}
            Dim i As Integer = 0
            Dim o As String = ""
            arabic(0) = 1000
            arabic(1) = 900
            arabic(2) = 500
            arabic(3) = 400
            arabic(4) = 100
            arabic(5) = 90
            arabic(6) = 50
            arabic(7) = 40
            arabic(8) = 10
            arabic(9) = 9
            arabic(10) = 5
            arabic(11) = 4
            arabic(12) = 1
            roman(0) = "M"
            roman(1) = "CM"
            roman(2) = "D"
            roman(3) = "CD"
            roman(4) = "C"
            roman(5) = "XC"
            roman(6) = "L"
            roman(7) = "XL"
            roman(8) = "X"
            roman(9) = "IX"
            roman(10) = "V"
            roman(11) = "IV"
            roman(12) = "I"
    
            While n > 0
    
                While n >= arabic(i)
                    n = n - arabic(i)
                    o = o & roman(i)
                End While
    
                i += 1
            End While
    
            Return o
        End Function
        Dim Input As Integer = 0
    
        Dim M As Integer = 0
        Dim MFoward As Integer = 0
        Dim MRomain As String
    
        Dim D As Integer = 0
        Dim DFoward As Integer = 0
        Dim DCromain As String
    
        Dim C As Integer = 0
        Dim CFoward As Integer = 0
    
        Dim L As Integer = 0
        Dim LFoward As Integer = 0
        Dim LXromain As String
    
        Dim X As Integer = 0
        Dim XFoward As Integer = 0
    
        Dim V As Integer = 0
        Dim VFoward As Integer = 0
        Dim VIroman As String
    
        Dim I As Integer = 0
        Dim VIromain As String
    
        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
            Input = txtNumbre.Text
            txtNumeral.Text = GetLoumaNumber(Input)
    
        End Sub
    
        Private Sub btnErase_Click(sender As Object, e As EventArgs) Handles btnErase.Click
            Me.txtNumbre.Text = "0"
            Me.txtNumeral.Text = "0"
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 29, 2018 5:58 AM
  • I have two issues with this code. First of all, when you erase the value that was converted, and input another number, it keeps the value that was used before and reputes it in with the newly converted number such as:

    4000 = MMMM

    I = MMMMI (Supposed to only be I)

    (It keeps the value that was converted and puts it in front of the next)

    My Next issues is it sometimes dosent include the conversion for the "V"s and the Xs and Ls, only when there not alone such as:

    5 = V

    1345 = MCCCIV (It leaves out the conversion for 40)

    Public Class frmRomain
        Dim Input As Integer = 0

        Dim M As Integer = 0
        Dim MFoward As Integer = 0
        Dim MRomain As String

        Dim D As Integer = 0
        Dim DFoward As Integer = 0
        Dim DCromain As String

        Dim C As Integer = 0
        Dim CFoward As Integer = 0

        Dim L As Integer = 0
        Dim LFoward As Integer = 0
        Dim LXromain As String

        Dim X As Integer = 0
        Dim XFoward As Integer = 0

        Dim V As Integer = 0
        Dim VFoward As Integer = 0
        Dim VIroman As String

        Dim I As Integer = 0



        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
            Input = txtNumbre.Text

            M = Input \ 1000
            MFoward = ((Input / 1000) - (Input \ 1000)) * 1000

            D = MFoward \ 500
            DFoward = ((Input / 500) - (Input \ 500)) * 500

            C = DFoward \ 100
            CFoward = ((Input / 100) - (Input \ 100)) * 100

            L = CFoward \ 50
            LFoward = ((Input / 50) - (Input \ 500)) * 50

            X = LFoward \ 10
            XFoward = ((Input / 10) - (Input \ 500)) * 10

            V = XFoward \ 5
            VFoward = ((Input / 5) - (Input \ 5)) * 5

            I = VFoward

            If (M = 1) Then
                MRomain = "M"
            End If

            If (C = 1) And (D = 0) Then
                DCromain = "C"
            ElseIf (C = 2) And (D = 0) Then
                DCromain = "CC"
            ElseIf (C = 3) And (D = 0) Then
                DCromain = "CCC"
            ElseIf (C = 4) And (D = 0) Then
                DCromain = "CD"
           ElseIf (D = 1) And (C = 0) Then
                DCromain = "D"
            ElseIf (D = 1) And (C = 1) Then
                DCromain = "DC"
            ElseIf (D = 1) And (C = 2) Then
                DCromain = "DCC"
            ElseIf (D = 1) And (C = 3) Then
                DCromain = "DCCC"
            ElseIf (D = 1) And (C = 4) Then
                DCromain = "CM"
            End If

            If (X = 1) And (L = 1) Then
                LXromain = "LX"
            ElseIf (X = 2) And (L = 1) Then
                LXromain = "LXX"
            ElseIf (X = 3) And (L = 1) Then
                LXromain = "LXXX"
            ElseIf (X = 4) And (L = 1) Then
                LXromain = "XC"
            ElseIf (L = 1) And (X = 0) Then
                LXromain = "L"

            ElseIf (X = 1) And (L = 0) Then
                LXromain = "X"
            ElseIf (X = 2) And (L = 0) Then
                LXromain = "XX"
            ElseIf (X = 3) And (L = 0) Then
                LXromain = "XXX"
            ElseIf (X = 4) And (L = 0) Then
                LXromain = "XL"
            End If


            If (I = 4) And (V = 1) Then
                VIromain = "IX"
            ElseIf (I = 3) And (V = 1) Then
                VIromain = "VIII"
            ElseIf (I = 2) And (V = 1) Then
                VIromain = "VII"
            ElseIf (I = 1) And (V = 1) Then
                VIromain = "VI"
            ElseIf (I = 4) Then
                VIromain = "IV"
            ElseIf (V = 1) Then
                VIromain = "V"
            ElseIf (I = 1) Then
                VIromain = "I"
            ElseIf (I = 2) Then
                VIromain = "II"
            ElseIf (I = 3) Then
                VIromain = "III"
            ElseIf (I = 4) Then
            End If

            txtNumeral.Text = MRomain & DCromain & LXromain & VIromain


        End Sub

        Private Sub btnErase_Click(sender As Object, e As EventArgs) Handles btnEraseClick

            Me.txtNumbre.Text = "0"
            Me.txtNumeral.Text = "0"
        End Sub
    End Class

    Some observations about the code you posted:

    (1) Always use the "Insert Code Block" button on the forum editor menu bar
    to post code. That will keep the formatting intact, add colors, etc. which
    will make the code much more readable.

    (2) When you post code use Windows Copy & Paste to copy the actual code
    you are compiling. Do not retype code into the forum editor as that 
    often leads to unintentional changes so we don't see the real code that
    you are using.

    (3) Check what you post for obvious errors. The code you posted would
    probably never compile so you could never actually be testing that code.
    It has errors such as:

    Dim VIroman As String

    All of the code that uses this variable has "VIromain" so this variable
    is incorrectly named.

    (4) You should be clearing the strings used for the results before every
    new calculation pass to erase any results from prior calculations. e.g. -

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Input = txtNumbre.Text
    
        MRomain = ""
        DCromain = ""
        LXromain = ""
        VIromain = ""
    
    

    Why are you dividing by 500 in these two lines?

    >LFoward = ((Input / 50) - (Input \ 500)) * 50

    >XFoward = ((Input / 10) - (Input \ 500)) * 10

    Perhaps you meant to do this instead?

    LFoward = ((Input / 50) - (Input \ 50)) * 50
    
    XFoward = ((Input / 10) - (Input \ 10)) * 10
    

    - Wayne

    Monday, October 29, 2018 7:57 AM
  • Hello,

    Here is a solution that utilizes recursion.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim values As New List(Of Integer) From {5, 1245}
    
        values.ForEach(Sub(item) Console.WriteLine(ToRoman(item)))
    End Sub
    Public Shared Function ToRoman(ByVal number As Integer) As String
        If (number < 0) OrElse (number > 3999) Then
            Throw New ArgumentOutOfRangeException("insert value betwheen 1 and 3999")
        End If
        If number < 1 Then
            Return String.Empty
        End If
        If number >= 1000 Then
            Return "M" & ToRoman(number - 1000)
        End If
        If number >= 900 Then
            Return "CM" & ToRoman(number - 900)
        End If
        If number >= 500 Then
            Return "D" & ToRoman(number - 500)
        End If
        If number >= 400 Then
            Return "CD" & ToRoman(number - 400)
        End If
        If number >= 100 Then
            Return "C" & ToRoman(number - 100)
        End If
        If number >= 90 Then
            Return "XC" & ToRoman(number - 90)
        End If
        If number >= 50 Then
            Return "L" & ToRoman(number - 50)
        End If
        If number >= 40 Then
            Return "XL" & ToRoman(number - 40)
        End If
        If number >= 10 Then
            Return "X" & ToRoman(number - 10)
        End If
        If number >= 9 Then
            Return "IX" & ToRoman(number - 9)
        End If
        If number >= 5 Then
            Return "V" & ToRoman(number - 5)
        End If
        If number >= 4 Then
            Return "IV" & ToRoman(number - 4)
        End If
        If number >= 1 Then
            Return "I" & ToRoman(number - 1)
        End If
        Throw New ArgumentOutOfRangeException("something bad happened")
    End Function


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, October 29, 2018 1:22 PM
    Moderator
  • Here is some code I've had for some time.  It has to / from functionality.

    Public Class RomanNumeral
        Enum Roman 'do not change
            M = 1000
            CM = 900
            D = 500
            CD = 400
            C = 100
            XC = 90
            L = 50
            XL = 40
            X = 10
            IX = 9
            V = 5
            IV = 4
            I = 1
        End Enum
    
        Private Shared Translation As List(Of Roman) = [Enum].GetValues(GetType(Roman)).OfType(Of Roman).OrderByDescending(Function(t) t).ToList
    
        Public Shared Function ToRoman(num As Integer) As String
            Dim rv As New System.Text.StringBuilder
            For Each t As Roman In Translation
                If num <= 0 Then Exit For
                While num >= t
                    num -= t
                    rv.Append(t.ToString)
                End While
            Next
            Return rv.ToString
        End Function
    
        Public Shared Function FromRoman(num As String) As Integer
            Dim rv As Integer
            For Each t As Roman In Translation
                If num = "" Then Exit For
                While num.StartsWith(t.ToString)
                    rv += t
                    num = num.Remove(0, t.ToString.Length)
                End While
            Next
            Return rv
        End Function
    
        Public Shared Function ToXMLTable() As XElement
            Dim root As XElement = <RomanNumerals></RomanNumerals>
            Dim entry As XElement = <number>
                                        <value r=""></value>
                                        <roman d=""></roman>
                                    </number>
            Dim rv As New XElement(root)
            For x As Integer = 1 To 3999
                Dim ent As New XElement(entry)
                ent.<value>.Value = x.ToString
                ent.<roman>.Value = ToRoman(x)
                ent.<value>.@r = ent.<roman>.Value
                ent.<roman>.@d = ent.<value>.Value
                rv.Add(ent)
            Next
            Return rv
        End Function
    End Class
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Monday, October 29, 2018 3:22 PM