-1

I have this code:

Module Module11
    Sub Main()
        Dim mess As String
        Dim out As String
        Dim num, p As UInteger
        Dim q As Integer = -1

        Console.Write("Enter a message: ")
        mess = Console.ReadLine()

        While p < 9
            q += 1
            num = Asc(mess(p + q)) + 5
            out = Chr(num)
            Console.Write(out)
        End While

        Console.ReadKey()
    End Sub
End Module

But on Line num = Asc(mess(p + q)) + 5, it is showing 'index out of the bounds of array'.

I was actually trying to create code that could change every character (even blank space) to the next sixth character (with reference to ASCII codes) of whatever character we input.

It shows error even after giving the correct output (in the black console).

Link to Pic of my error

Please help.

  • `p` never changes, so you have an infinite loop where `q` keeps increasing until `p + q` is out of bounds. – Ry- Sep 15 '18 at 03:13
  • The length of mess is exceeded in your infinite loop. The array of char that is the string mess has an upper bound of mess.length -1. – Mary Sep 15 '18 at 06:10

1 Answers1

0

I think I have the answer (or at least AN answer). I'm not familiar with visual basic, but I am familiar with Python and this seems to be extremely similar. After doing some quick researching, I learned that the "UInteger" data type cannot be negative. I would recommend changing the while loop to read "While p > 0 And p < 9" and then the rest of the code. It looks to me like your answer is coming out negative. This would make sense considering what your error message says. However, I'm not sure how the output would still come out correctly, but it's worth a shot. I would try that, and let me know how it goes!

Faith
  • 16
  • 1