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

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




Arrays

 
Reply to this topicStart new topic

Arrays

KKKKK
16 Feb, 2007 - 04:08 PM
Post #1

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 15


My Contributions
Original post by Dalit:

Hey I'm making a program for my Computing Class that does this

1. Stores 5 usernames in an array on strings
2. Asks user for their ID then validates it
3. Asks them for their desired seat number
4. Displays booking details.

Problem is, one user can keep going around in a loop and book every available seat. I want to display something that says "You have already booked a seat, would you like to change your seat?

Not sure how I would do this, I tried doing it with 2-D arrays but I only done a little on those and I'm not sure how to go about doing so.

Here is my code (I tidied it up as much as I could :S)

CODE
Private Sub Command1_Click()
'Declare Variables
Dim ids(5)
Dim responce As Integer
Dim bookings(5)
Dim inputval(5)
'Call Sub Routines
Call create_arrays(ids(), flightnr, bookings())
    Do
        Call check_id(ids(), pasid, inputval())
        Call book_seat(bookings(), pasid, seatnr, ids())
        Call display_results(flightnr, pasid, seatnr)
        'Ask user if they want to exit
        responce = MsgBox("Do you need to enter another value?", vbYesNo, "Exit?")
    Loop Until responce = vbNo


End Sub


Private Sub create_arrays(ByRef ids(), flightnr, bookings())
'Create arrays/store flight details
ids(0) = "WallaceW48P01"
ids(1) = "LauderH468P02"
ids(2) = "CarnegieA468P3"
ids(3) = "ScottW468P04"
ids(4) = "DeBruisR468P05"
flightnr = "HA468"
bookings(1) = ""
bookings(2) = ""
bookings(3) = ""
bookings(4) = ""
bookings(5) = ""
End Sub


Private Sub check_id(ByRef ids(), pasid, inputval())
'Declare Variables
Dim found As Boolean
Dim counter As Integer
Dim counter2 As Integer
'Get user Input
pasid = InputBox("Enter your user ID")
'Input Validations
  Do
  
     For counter = 0 To 4
        If ids(counter) = pasid Then
        found = True
        MsgBox ("The ID you was accepted")
    
        If Not found Then MsgBox ("The ID you entered was invalid")
        Else
        End If
    Next
  Loop Until found = True
End Sub

Private Sub book_seat(ByRef bookings(), ByVal pasid, ByRef seatnr, ids())
'Declare variables
Dim found As Boolean
'Get input
seatnr = InputBox("Enter your desired seat number Between 1 and 5")
'Input Validation
    Do
        Do
          If seatnr < 1 Or seatnr > 5 Then
            seatnr = InputBox("That was an invalid number, Please enter your desired seat number Between 1 and 5")
          End If
        Loop Until seatnr >= 1 And seatnr <= 5
       If bookings(seatnr) = "" Then
           found = True
           bookings(seatnr) = "taken"
       Else
           MsgBox ("seat number " & seatnr & " was not available")
       End If
    Loop Until found = True
    If Not found Then MsgBox ("Your seat has been booked")
End Sub



Private Sub display_results(ByVal flightnr, pasid, seatnr)
'Declare Variables
Dim pasid2 As String
'string concatenation
pasid2 = Mid$(pasid, 1, Len(pasid) - 6)
'Display Results
txtbox.AddItem "            Booking Confirmation"
txtbox.AddItem "Flight Number :" & flightnr
txtbox.AddItem "ID            :" & pasid
txtbox.AddItem "Name          :" & pasid2
txtbox.AddItem "Seat Number   :" & seatnr
End Sub



I am also doing this program and having a hell of a lot of problems. I was wondering if anyone could perfect this code until it works the best they got in the other topic was:

CODE
Private Sub book_seat(ByRef bookings(), ByVal pasid, ByRef seatnr, ids())
Dim found As Boolean
Dim answer As Integer
  Do
  
  seatnr = InputBox("Enter your desired seat number Between 1 and 5")
  'Input Validation x > 1, x < 5
  Do
       If seatnr < 1 Or seatnr > 5 Then
           seatnr = InputBox("That was an invalid number, Please enter your desired seat number Between 1 and 5")
       End If
   Loop Until seatnr >= 1 And seatnr <= 5
   'Check if seat is available
   Dim seatsBooked As Integer
   seatsBooked = 0

' Loop through each booking and count number times pasid has booked a seat
   For a = 0 To 4
      If bookings(a) = pasid Then
           seatsBooked = seatsBooked + 1
      End If
   Next
' Make sure user hasn't previously booked a seat
   If seatsBooked = 0 Or seatsBooked = 99 Then
        If bookings(seatnr) = "" Then
             found = True
             bookings(seatnr) = pasid
             MsgBox ("Seat " & seatnr & " was booked for: " & pasid)
        Else
             MsgBox ("seat number " & seatnr & " was not available, please enter a number between 1 and 5")
        End If
    Else
'   Ask user if they want to change their booked seat.
         MsgBox ("A seat has already been booked for your UserID: " & pasid)
         answer = MsgBox("Do you want to change your booked seat?", vbYesNo, "Previous booking detected")
            If responce = vbYes Then
                seatsBooked = 98
                Call book_seat(bookings(), pasid, seatnr, ids())
            Else
                End If
    End If
  Loop Until found = True
End Sub



I have no clue what the response part does also so could someone explain this.
User is offlineProfile CardPM
+Quote Post

orangeslide8
RE: Arrays
18 Feb, 2007 - 10:11 AM
Post #2

D.I.C Head
Group Icon

Joined: 29 Dec, 2006
Posts: 190



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

dim username (5)
dim password(5)
dim whichnumber as integer
dim username2(5) as boolean
private sub form1_load()
setusernamesandpasswords()
setusername2asfalse()
end sub
private sub setusername2asfalse()
username2(1) = false
username2(2) = false
username2(3) = false
username2(4) = false
username2(5) = false
end sub
private sub setusernamesandpasswords()
username(1) = "Bob"
Password(1) = "bob" ' could use anything u want
username(2) = "john"
Password(2) = "John"
username(3) = "Jill"
Password(3) = "Jil"
username(4) = "jack"
password(4) = "jack"
username(5) = "Susan"
passowrd(5) = "susan"
end sub
private sub button1_click()
If Textbox1.Text = (Username(1)) then
whichnumber = 1
checkifused()
CheckPassword()
end if
If Textbox1.Text = (Username(2)) then
whichnumber = 2
checkifused()
CheckPassword()
end if
If Textbox1.Text = (Username(3)) then
whichnumber = 3
checkifused()
CheckPassword()
end if
If Textbox1.Text = (Username(4)) then
whichnumber = 4
checkifused()
CheckPassword()
end if
If Textbox1.Text = (Username(5)) then
whichnumber = 5
checkifused()
CheckPassword()
end if
If Textbox1.Text <> (Username(1)) or Textbox1.Text <>(Username(2)) or Textbox1.text <> (Username(3)) or Textbox1.text <> Username(4)) or Textobx1.Text <> Username(5)) then
msgbox ("Invalid username")
end sub
private sub CHeckpassword()
If TextBox2.Text = (Password(Whichnumber)) then
Msgbox("he has a seat")
else
msgbox("Invalid Password")
end sub
private sub Checkifused()
if Username2(Whichnumber)) = True then
msgbox("You already have a seat")
end sub

hope that helped
john

This post has been edited by orangeslide8: 18 Feb, 2007 - 10:12 AM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Arrays
18 Feb, 2007 - 01:00 PM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
The first BIG problem that I see is in the do-loop in Sub check_id

CODE

Do
    'loop though user ID's
    For counter = 0 To 4
        'If ID is in array then...
        If ids(counter) = pasid Then
        found = True
        MsgBox ("The ID you was accepted")
        '***** This line makes no sence... how can found=FALSE ever
        '*****    when 2 line above it is assigned TRUE!!!
        If Not found Then MsgBox ("The ID you entered was invalid")
        Else
        '***** Else what?
        End If
    Next
'**** um... if the user does not enter a valid ID then should found ever be true?
Loop Until found = True


I really don't see how that worked for you. First of all the DO-LOOP is not really needed, to make it work you would need to add in input step so that the pasid var could change (and therefore one day actually be a correct input).

The For-Next loop was the way to go...

CODE

    'notice there is no DO-LOOP
    FOR counter = 0 TO 4
        IF ids(counter) = pasid THEN
             'Ok it is a valid ID...
             found = True
             'No need to check any other ID's
             EXIT FOR
        END IF
    NEXT
    IF found THEN
        MsgBox ("The ID you was accepted")
    ELSE
        MsgBox ("The ID you entered was invalid")
    END IF


Secondly this is the type of thing that makes a really good function

CODE

Private Function IsValidID(pasid) as Boolean
    FOR counter = 0 TO 4
        IF ids(counter) = pasid THEN
             'Ok it is a valid ID...
             found = True
             'No need to check any other ID's
             EXIT FOR
        END IF
    NEXT
   IsValidID = found
End Function

'Your Do-Loop now becomes
If IsValidID(pasid) Then
       MsgBox ("The ID you was accepted")
Else
        MsgBox ("The ID you entered was invalid")
End If



Although I am not sure that is how you should do it. If you need to check the same ID over and over then it would be best to store the result into a boolean var rather than call the function over and over.

To get back to your original question: How to tell when a given ID has a booking...
You could:
1. Add a second dimention to the Ids array and use that to tell if the user has a seat.
2. When a seat is booked add the index of the passenger ID (for the Ids array) to the bookings
and then For-Next loop though the seats to see if that passenger has already made a selection.


I am sure that there are meny more options.

I hope that this is helpful
User is offlineProfile CardPM
+Quote Post

KKKKK
RE: Arrays
18 Feb, 2007 - 01:05 PM
Post #4

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 15


My Contributions
Thanks Nick but I've altered the code greatly, please look at http://www.dreamincode.net/forums/showtopic24076.htm for my updated code. Would be much appreciated thankyou.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Arrays
18 Feb, 2007 - 01:37 PM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
The problem might be solved by assigning the pasid instead of the word "taken" into the booking array.
Also you might want to add in an ElseIF to tell the user if they are trying to double book thier seat...

CODE

Private Sub book_seat(ByRef bookings(), ByVal pasid, ByRef seatnr, ids())
'Declare variables
Dim found As Boolean
Dim Counter as Integer
'First of all lets find out if the user has a seat already
For Counter = 1 to 5
   If bookings(Counter)=pasid Then found = true
Next Counter
If Found then
       MesgBox ("You have already booked a seat of this flight!")
   Else
       'Note that we know Found=false to be in this branch...
       seatnr = InputBox("Enter your desired seat number Between 1 and 5")  
       'Input Validation
       Do
           'Validate the seat number...
           Do
               If seatnr < 1 Or seatnr > 5 Then
                    seatnr = InputBox("That was an invalid number," & _
                         "Please enter your desired seat number Between 1 and 5")
               End If
           Loop Until seatnr >= 1 And seatnr <= 5
          
           If bookings(seatnr) = "" Then
               found = True
               bookings(seatnr) = pasid
           Else
               MsgBox ("seat number " & seatnr & " was not available")
           End If
       Loop Until found = True
       MsgBox ("Your seat has been booked")
   End If
End Sub


I hope that this helps
User is offlineProfile CardPM
+Quote Post

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

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