Hello, I'll be as clear as I can in this one. OK, here is what is going on: I'm kind of trying to create a programming language of some sort using a Console application. I have got most commands to work, but what I haven't got is the ability to perform addition of two numbers that are in the same line and display the result in the next line. Before I show you the "problematic" code, you better take a look at this so to better understand the rest of the code:

Dim input As String = Console.In.ReadLine
      Dim startIndex As Integer
      Dim output As String)

Here is the rest of the code:

If input.Contains("+") Then
            Try
                startIndex = input.IndexOf("+")
                Dim firstNum As Integer = input.Substring(startIndex - 1, 1)
                Dim secondNum As Integer = input.Substring(startIndex + 1, 1)
                output = firstNum + secondNum
                WriteLine("=> " & output)
                input = "" : GoTo repeat
            Catch ex As Exception
                WriteLine(ex.Message)
                input = "" : GoTo repeat
            End Try
        End If

The main problem with the code is that, while the program can perform addition of numbers with one digit, for numbers greater than nine it won't. I know this has to do with the specified length in the above code. The part with the problem is (startIndex - 1, 1), as well as (startIndex + 1, 1). I know I could just increase the length to greater values but, if I did this, I would then be required to type in a number with that number of digits no matter what. For example, if I set the length to 3, I would have to type in a three digit number in order for this to work, else it would throw an exception. Do you have any ideas regarding how I can solve this?

Recommended Answers

All 2 Replies

I'm not a console coder, but this should help.

Dim sInput As String = "250+1" '// value for testing.
        Dim iIndex As Integer = sInput.IndexOf("+") '// locate the "+".
        Dim iFirstNum As Integer = CInt(sInput.Substring(0, iIndex)) '// (startIndex, Length)
        Dim iSecondNum As Integer = CInt(sInput.Substring(iIndex + 1)) '// (startIndex and everything after)
        Dim sOutput As String = CStr(iFirstNum + iSecondNum)
        MsgBox("=> " & sOutput) '// Display result.
commented: Exactly what I was looking for! Problem solved! +2

Thank you very much! The problem is now solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.