I would like to know how to take input from a user and populate an array?

I know how to do an array normally. I know how to have a user input information into a file that I create. But, I don't know how to put the two together.

Any help would be appreciated!

Recommended Answers

All 3 Replies

First Dimension an array with the required number of elements. Design a form with textbox or something to accept input.
Form a loop to run up to the size of the array. Inside the loop get the imput from the user, and the assign stright to the array element with the index.

If you are using a file. Try to write it from the input each line output.
(Otherwise you have to seperate two data with some delimiters.)

open this file in input mode
Run a loop either to EOF or to the size of your dimensioned array.
inside the loop
read the first line from the file
just assign to each elements of the array.
End loop
OVER

'hope this code snippet can serve your purpose

Dim n(4), s As String, i As Integer

'populating the string array
For i = 0 To 4 Step 1
begin:
s = InputBox("Demo of populating an array by accepting name of 5 students." & Chr(10) & "Enter name of student" & i + 1 & " :", "Populating Array")
If s <> "" Then
n(i) = s
Else
MsgBox "No student has null in name."
GoTo begin
End If
Next i

'creating and writing array values into file
Open App.Path & "\name.txt" For Output As #1
For i = 0 To 4 Step 1
Print #1, "Information About Student"
Print #1, "---------------------------------"
Print #1, "Name : " & n(i)
Print #1, "Position in array : " & i
Print #1, "Holding rank : " & i + 1
Print #1, "---------------------------------"
Print #1, ""
Next i
Close #1

Dim arr() As String

Private Sub Command1_Click()Static i As Integer

ReDim Preserve arr(i)

arr(i) = Me.Text1

i = i + 1
End Sub

Private Sub Command2_Click()Dim i As Integer

Me.Cls

For i = 0 To UBound(arr)Me.Print arr(i)
Next i

End Sub

'Hope it will help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.