This is just a piece of my code, but it’s causing me issues. The intended purpose of the code is to take the users input and make it the size of the array. However, it is giving me the error ‘index was outside bounds of array’ not matter what value I enter.
Here is the code:
Option Explicit On
Option Strict On
Imports System
Module numbers
Sub Main()
'index decides number of candidates.
Dim index as integer
Dim candidate(index) as integer
Console.Write("Please enter the number of candidates in the election: ")
index=Convert.toInt32(Console.Readline())
Do Until candidate(index) >= 0
Console.Write(" Enter the name of candidate: ")
candidate(index)=Convert.toInt32(Console.Readline())
candidate(index) -=1
Loop
End Sub
End Module
You’ve got a few problems here.
Firstly, you need to initialise your array after you know how big it is.
Secondly, you will find a For loop is easier than a Do loop for implementing the logic, as you don’t need to keep track of the loop counter manually.
Finally, you are converting your candidate names to an integer. Most people’s names are not numeric!