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
                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
                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?

Edited by TechSupportGeek: n/a

6 Years
Discussion Span
Last Post by TechSupportGeek

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.
Votes + Comments
Exactly what I was looking for! Problem solved!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.