Welcome to Dream.In.Code
Become a VB Expert!

Join 137,228 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,893 people online right now. Registration is fast and FREE... Join Now!




Populating an Array

 
Reply to this topicStart new topic

Populating an Array

riz
8 Mar, 2007 - 12:39 PM
Post #1

New D.I.C Head
*

Joined: 8 Mar, 2007
Posts: 3


My Contributions
Hi there!

Im just new so bare with me here...

Right, im having a little trouble with a project here. I need to populate my array, without displaying the information. And then, using an inputbox, validate the ID that is entered. If the ID is entered wrongly, an error message should appear. If it corresponds with one of the stored ID's in my array, it should allow me to proceed to the next step (which is a problem i'll overcome if and when i come to it!)

My coding skills are pretty pathetic. But heres what i got so far...

CODE

Private Sub cmdid_Click()

Dim valid_id(4) As Double
Dim details As Double
Dim i As Integer

valid_id(0) = "WallaceW"
valid_id(1) = "CarnegieA"
valid_id(2) = "ScottW"
valid_id(3) = "LauderJ"
valid_id(4) = "MillsS"

For i = 0 To 4

Do
    details = InputBox("Please enter your passenger ID")
    If valid_id = "WallaceW" Or valid_id = "CarnegieA" Or valid_id = "ScottW" Or valid_id = "LauderJ" Or valid_id = "MillsS" Then
    MsgBox ("Your ID is valid. You must book a seat")
    Else
    MsgBox ("Your ID is invalid")
    End If
Loop Until details = "WallaceW" Or details = "CarnegieA" Or details = "ScottW" Or details = "LauderJ" Or details = "MillsS"
Next

End Sub


I think thats pretty messy coding. But its the best i can do! I keep getting error messages all over the shop, and im just stuck lol.

Thanks.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Populating An Array
8 Mar, 2007 - 12:49 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
First and foremost, you have declared your array to be af variable type Double, which is a numerical type. You are then attempting to assign text, or string values to the elements. It appears as if you may require an array of strings, not doubles.

Secondly, when you reference an array element, you need to reference it by index, not by the name of the array:
CODE

IF valid_id(0)="text"

for example, as opposed to
CODE

If valid_id="text"

User is offlineProfile CardPM
+Quote Post

orangeslide8
RE: Populating An Array
11 Mar, 2007 - 04:58 AM
Post #3

D.I.C Head
Group Icon

Joined: 29 Dec, 2006
Posts: 190



Thanked: 1 times
Dream Kudos: 25
My Contributions
i would do something like this
CODE

Private Sub cmdid_Click()

Dim valid_id(4) As string
Dim details As Double
Dim i As Integer

valid_id(0) = "WallaceW"
valid_id(1) = "CarnegieA"
valid_id(2) = "ScottW"
valid_id(3) = "LauderJ"
valid_id(4) = "MillsS"

For i = 0 To 4
if inputbox1.text = (valid_id(0)) or inputbox1.text = (valid_id(1)) or inputbox1.text = (valid_id(2)) or inputbox1.text = (valid_id(3)) or inputbox1.text = (valid_id(4)) then
MsgBox ("Your ID is valid. You must book a seat")
else
msgbox("Your ID is invalid")
Next
End Sub


User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Populating An Array
11 Mar, 2007 - 06:56 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Since you've implemented a loop, I'd suggest that you use the value of the counter i to identify the elements instead of straight one on one comparisons - the loop you've shown performs the same check 5 times.
User is offlineProfile CardPM
+Quote Post

orangeslide8
RE: Populating An Array
11 Mar, 2007 - 10:03 AM
Post #5

D.I.C Head
Group Icon

Joined: 29 Dec, 2006
Posts: 190



Thanked: 1 times
Dream Kudos: 25
My Contributions
QUOTE(Amadeus @ 11 Mar, 2007 - 07:56 AM) *

Since you've implemented a loop, I'd suggest that you use the value of the counter i to identify the elements instead of straight one on one comparisons - the loop you've shown performs the same check 5 times.

right my bad heres a better one
CODE

Private Sub cmdid_Click()

Dim valid_id(4) As string
Dim details As Double
Dim i As Integer

valid_id(0) = "WallaceW"
valid_id(1) = "CarnegieA"
valid_id(2) = "ScottW"
valid_id(3) = "LauderJ"
valid_id(4) = "MillsS"

For i = 0 To 4
if inputbox1.text = (valid_id(i)) then
MsgBox ("Your ID is valid. You must book a seat")
else
msgbox("Your ID is invalid")
Next
End Sub


This post has been edited by orangeslide8: 11 Mar, 2007 - 10:04 AM
User is offlineProfile CardPM
+Quote Post

riz
RE: Populating An Array
13 Mar, 2007 - 10:57 AM
Post #6

New D.I.C Head
*

Joined: 8 Mar, 2007
Posts: 3


My Contributions
Thanks for all your help smile.gif

With a few little tweaks by the teacher, ive ended up with a programme looking like this...

CODE

Private Sub cmdbook_click()

'declare variables
Dim validid(5) As String
Dim seat(5) As Integer
Dim counter As Integer
Dim flightnumber As String
Dim seatposition As Integer
Dim seatnumber As Integer
Dim passengerid As String

'store details
validid(1) = "WallaceW48P01"
validid(2) = "LauderH468P02"
validid(3) = "CarnegieA468P3"
validid(4) = "ScottW468P04"
validid(5) = "DeBruisR468P05"
flightnumber = "HA468"


'input validation of passengers ID
Do
passengerid = InputBox("Please enter your passenger ID")
    For counter = 0 To 4
        If passengerid = validid(counter) Then
         MsgBox ("Your passenger ID is valid, you must now book a seat")
        If passengerid <> validid(counter) Then
         MsgBox ("Your passenger ID is not valid. Please re-enter")
        End If
        End If
    Next
Loop Until passengerid = validid(counter)

seat(1) = seatnumber
seat(2) = seatnumber
seat(3) = seatnumber
seat(4) = seatnumber
seat(5) = seatnumber


'validating seat number then displaying flight details in listbox
Do
seatposition = InputBox("Please book a seat number between 1 and 5")
    If seatposition >= 1 Or seatposit/on <= 5 Then
     lstdetails.AddItem "passengerid"
     lstdetails.AddItem "flightnumber"
     lstdetails.AddItem "seatnumber"
    End If
    If seatposition < 1 Or seatnumber > 5 Then
     MsgBox ("You have entered an invalid seat number. Please try again.")
    End If
Loop Until seatposition >= 1 Or seatnumber <= 5
    
End Sub


Even when it type in the correct passenger ID, it still loops and asks for it again and again. confused.gif
I dont understand whats wrong with it!

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Populating An Array
13 Mar, 2007 - 11:04 AM
Post #7

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
You have to break out of the inner loop if the ID is valid.
CODE

If passengerid = validid(counter) Then
   Exit For

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 03:00PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month