Question Help with message encryption task, using an array

DerpBane

New member
Joined
Nov 26, 2013
Messages
4
Programming Experience
Beginner
Hi,
So I've been trying to get through this task for awhile now, for my college work, and there's something that I'm not getting. Here is the task:
Write a program that declares an array of characters with six rows and six columns and fills it with ?space? characters.
It should then input a message, place it letter by letter into the array by rows, then output the result of reading it column by column.
When testing your program try to use messages that are close to the maximum length or your encrypted version will contain many blanks.
When copying down the encrypted version, count the spaces carefully.
Now here is my attempt, please forgive how ugly it looks, but it is the result of a lot of changing and testing:
Module Module1


    Sub Main()
        Dim Message(5, 5) As String
        Dim number1 As Integer
        Dim number As Integer
        Dim StringLine, EncryptedString As String
        For Index2 = 0 To 5
            For Index = 0 To 5
                Message(Index, Index2) = ""


            Next Index


        Next Index2


        Console.Write("Insert a message <up to 36 characters>: ")
        StringLine = Console.ReadLine


        Dim Testnum As Integer


        Testnum = 36 - StringLine.Length
        If Testnum > 0 Then
            For num = 0 To Testnum
                StringLine = StringLine + (" ")
            Next num


        End If


        number = -1
        For Index2 = 0 To 5
            number = number + 1
            For Index = 0 To 5
                number1 = number
                Message(Index2, Index) = StringLine.Substring(number1)
                number1 = number1 + 1
            Next Index
            number = number1
        Next Index2


        EncryptedString = ""
        For Index2 = 0 To 5
            For Index = 0 To 5
                EncryptedString = EncryptedString + Message(Index, Index2)
            Next Index
        Next Index2




        Console.WriteLine(EncryptedString)
        Console.ReadLine()
    End Sub


End Module





When I try to run it, I usually get greeted by an encrypted message that is far longer than my starting message. Technically, I can move on from this task, but it's annoying me.

thanks
 
Last edited by a moderator:
Hi,

I can see a few issues here but there are two main things that are causing you your problems. These are:-

1) The first issue is that you have declared your array as a String Array so you are able to store more that one character in each element of the array. I would suggest that you change this to an array of Char and then turn Option Strict On. This will then point you to two assignment errors in your code which you can solve using the CChar Function.

2) The second issue is your use of Substring. You are using the first Overload of the Method that returns ALL the characters of String from a Starting Point. You need to be using the second overload of that method which also takes an argument specifying the number of characters to be returned from the starting point. Have a look here:-

String.Substring Method (System)

Hope that helps.

Cheers,

Ian
 
Back
Top