CIS 220 - Assignment 5:
Debugging Program

Objectives: Gain experience with debugging functions, subs, and parameters.

Description: This program is intended to provide you with experience debugging a program that has multiple errors associated with its functions and subs.


The code below should be typed in (or copied and pasted) and debugged so that it runs without errors, produces correct results, and conforms to good programming standards.  See the demo for correct results.  Note that the demo does not have complete error checking and should not be used as a standard for your error checking efforts.

The program contains several errors, both in syntax and in logic.  Some of the required modifications include:

The code below can be copied and pasted directly into VB. 

'-----------------------------------------------------------------------------------------------------
' Encode/Decode using a control array.
'
' This program will encode or decode a four digit integer number.
' - To encode a number, each digit is replaced by the sum of that
' digit plus 7 (the parameter num) modulus 10.
' - To decode a number, each digit is replaced by the sum of that
' digit plus 3 (the parameter num)modulus 10.
' - The first digit will be swapped with the third.
' - The second digit will be swapped with the fourth.
'-----------------------------------------------------------------------------------------------------
Private Sub cmdEncode_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdEncode.Click
Dim key As Integer = 7
Call selectEncodeOrDecode(key)
End Sub

Private Sub cmdDecode_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdDecode.Click
Dim key As Integer = 3
Call selectEncodeOrDecode(key)
End Sub

Private Sub selectEncodeOrDecode(ByVal key As Integer)
Dim number As Integer
Dim digit1 As Integer, digit2 As Integer, _
digit3 As Integer, digit4 As Integer

If NumberLengthIs4() And NumberIsPositive(Val(txtNumber.Text)) Then
number = txtNumber.Text
Call ExtractDigits(number, digit1, digit2, digit3, digit4)
Call Swap(digit1, digit3)
Call Swap(digit2, digit4)
Call Decrypt(key, digit1, digit2, digit3, digit4)
'number = CombineDigits(digit1, digit2, digit3, digit4)
txtResult.Text = Format(number, "0000")
Else ' errors detected
Call textHighlighter(txtNumber)
End If
End Sub

'-----------------------------------------------------------------------------------------------------
' This procedure checks to be sure that a four digit number has been entered.
' It returns True if it has and False if not.
'
' incoming parameters:
' outgoing parameters:
'-----------------------------------------------------------------------------------------------------
Private Function NumberLengthIs4() As Byte
NumberLengthIs4 = (Len(txtNumber.Text) = 4)
If Not NumberLengthIs4 Then MsgBox("Please enter a four digit number")
End Function

'-----------------------------------------------------------------------------------------------------
' This procedure checks to be sure that a positive number has been entered.
' It returns True if it has and False if it hasn't.
'
' incoming parameters:
' outgoing parameters:
'-----------------------------------------------------------------------------------------------------
Private Function NumberIsPositive(ByRef num As Integer) As Boolean
If (num >= 0) Then
NumberIsPositive = False
num = num * 2
Else
MsgBox("Please enter a positive number")
NumberIsPositive = True
End If
End Function
'-----------------------------------------------------------------------------------------------------
' This procedure accepts a four digit number (argument num) and returns
' each of the individual digits.
'
' incoming parameters:
' outgoing parameters:
'-----------------------------------------------------------------------------------------------------
Private Sub ExtractDigits(ByVal num As Integer, ByVal digit1 As Integer, _
ByVal digit2 As Integer, ByVal digit3 As Integer, ByVal digit4 As Integer)
digit1 = (num \ 1000)
digit2 = (num - (digit1 * 1000)) \ 100
digit3 = (num - (digit1 * 1000) - (digit2 * 100)) \ 10
digit4 = (num - (digit1 * 1000) - (digit2 * 100) - (digit3 * 10)) \ 1
End Sub

'-----------------------------------------------------------------------------------------------------
' This procedure accepts four individual digits, converts them to their
' four digit numerical equivalent, and returns the result.
'
' incoming parameters:
' outgoing parameters : none (aside from return value)
'-----------------------------------------------------------------------------------------------------
Private Function CombineDigits(ByVal digit1 As Integer, ByVal digit2 As Integer, _
ByVal digit3 As Integer, ByVal digit4 As Integer) As Byte
CombineDigits = digit1 * 1000 + digit2 * 100 + digit3 * 10 + digit4
End Function

'-----------------------------------------------------------------------------------------------------
' This procedure accepts two arguments (any type) and swaps them. Note: Object
' type is used intentionally here so that Swap can be used with any argument
' type. It can be used to swap two Integers, two Singles, two characters, or
' two of anything!
'
' incoming parameters: digitA -- initial first digit
' digitB -- initial second digit
' outgoing parameters: digitA -- final first digit (was second digit)
' digitB -- final second digit (was first digit)
'-----------------------------------------------------------------------------------------------------
Private Sub Swap(ByRef digitA As Object, ByVal digitB As Object)
Dim temporary As Object
temporary = digitA
digitA = digitB
digitB = digitA
End Sub
'-----------------------------------------------------------------------------------------------------
' This procedure will either encode or decode a number.
' - To encode a number, each digit is replaced by the sum of that digit
' plus 7 (the parameter num) modulus 10.
' - To decode a number, each digit is replaced by the sum of that digit
' plus 3 (the parameter num) modulus 10.
' Each individual digit is returned.
'
' incoming parameters: num -- the encoding value (added to each digit)
' outgoing parameters: digit1 -- the first encoded digit
' digit2 -- the second encoded digit
' digit3 -- the third encoded digit
' digit4 -- the fourth encoded digit
'-----------------------------------------------------------------------------------------------------
Private Sub Decrypt(ByVal num As Integer, ByVal digit1 As Integer, _
ByVal digit2 As Integer, ByVal digit3 As Integer, ByVal digit$ As Integer)
digit1 = (digit1 + num) Mod 10
digit2 = (digit2 + num) Mod 10
digit3 = (digit3 + num) Mod 10
digit4 = (digit4 + num) Mod 10
End Sub

'-----------------------------------------------------------------------------------------------------
' This procedure accepts a text box as a parameter, sets the focus to that box,
' places the cursor at the beginning of the box, and highlights the text.
'
' incoming parameters: txtVar -- the form variable that contains the invalid data
' outgoing parameters: none
'-----------------------------------------------------------------------------------------------------
Private Sub textHighlighter(ByVal txtVar As TextBox)
txtVar.Focus()
txtVar.SelectionStart = 0 ' Set cursor to beginning of box
txtVar.SelectionLength = Len(txtVar.Text) ' Highlight text
End Sub

The interface should look like the following.  See the demo for an example of the functionality of this program.

You do not need to submit pseudocode for this program.