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

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




2-D Arrays?

2 Pages V  1 2 >  
Reply to this topicStart new topic

2-D Arrays?

Dalit
12 Dec, 2006 - 12:38 PM
Post #1

D.I.C Head
**

Joined: 24 Jan, 2006
Posts: 54


My Contributions
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

User is offlineProfile CardPM
+Quote Post

Dalit
RE: 2-D Arrays?
13 Dec, 2006 - 07:44 AM
Post #2

D.I.C Head
**

Joined: 24 Jan, 2006
Posts: 54


My Contributions
Here's a bump and my attempt at the code

CODE
Private Sub check_id(ByRef ids(), pasid, inputval())
'Declare variables
Dim found As Boolean
Dim counter As Integer
Dim counter2 As Integer
   Do
   'Prompt user for input
   pasid = InputBox("Enter your user ID")
   'Ask if they want to change their booked seat
      Do
       For counter2 = 0 To 4
        If inputval(pasid) = "taken" Then
           responce = MsgBox("You have already booked a seat, would you like to change your seat?", vbYesNo, "Exit?")
            If responce = vbYes Then
                   inputval(pasid) = pasid
            End If
           Else
        End If
       Next
      Loop Until responce = vbNo

   'Check if the ID is valid
   For counter = 0 To 4
       If ids(counter) = pasid Then
       found = True
       MsgBox ("The ID you was accepted")
       inputval(pasid) = "taken"
       If Not found Then MsgBox ("The ID you entered was invalid")
   Else
End If
Next
Loop Until found = True
End Sub

User is offlineProfile CardPM
+Quote Post

Mach1Guy
RE: 2-D Arrays?
15 Dec, 2006 - 06:22 AM
Post #3

D.I.C Head
Group Icon

Joined: 4 Dec, 2006
Posts: 79



Thanked: 4 times
Dream Kudos: 25
My Contributions
Dalit, I am at work, so I don't have time to go through your code at the moment. But, a possible solution, seeing how you only have 5 users, could be to set a flag on the ID when the user books a seat. If you don't want to use a 2d array you could just create another array that contained the IDs of users who have already booked a seat. That is just a quick possible solution. Let me know if this works for you or you want some other solution. Good luck
User is offlineProfile CardPM
+Quote Post

Dalit
RE: 2-D Arrays?
15 Dec, 2006 - 01:37 PM
Post #4

D.I.C Head
**

Joined: 24 Jan, 2006
Posts: 54


My Contributions
Cheers Match1Guy, Problem is I'm not sure how to go about setting a 'flag' on the User. I'll try google to see if I can find anything relevant.

And what you suggested secondly was what I attempted. But I asked my teacher and she said I would need to do some sort of search through the array since it corresponds to numbers and not strings.

She was kind of relentless to help me since it went beyond what was needed done but Id still like to do it
User is offlineProfile CardPM
+Quote Post

Mach1Guy
RE: 2-D Arrays?
15 Dec, 2006 - 02:43 PM
Post #5

D.I.C Head
Group Icon

Joined: 4 Dec, 2006
Posts: 79



Thanked: 4 times
Dream Kudos: 25
My Contributions
You appear to have many small bugs and errors within your code. If you could repost all your code that you have so far for this assignment I would be glad to help you. Also, what version of Visual Basic do you have? The best way to debug a program and figure out problems is to just step through the code. Off hand here are 2 things I noticed right away.

Just as a reminder when you declared your ids array, the length of the array is 6 and you only need an array of 5. When you declare it with ids(5) that creates 0 - 6, not a length of 5.

The below code does not work correctly. It is an infinite loop. You should be able to debug yourself and figure out why.
CODE

        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


Please try debugging your code and then repost all your code and we will try assisting you. If you need help just let me know.

This post has been edited by Mach1Guy: 15 Dec, 2006 - 02:44 PM
User is offlineProfile CardPM
+Quote Post

Dalit
RE: 2-D Arrays?
15 Dec, 2006 - 06:19 PM
Post #6

D.I.C Head
**

Joined: 24 Jan, 2006
Posts: 54


My Contributions
Fixed the array thing. Don't know why it's going into an infinite loop

CODE
Private Sub Command1_Click()
'Dim Variables
Dim ids(4)
Dim responce As Integer
Dim bookings(4)
'Call Subroutines
Call create_arrays(ids(), flightnr, bookings())
   Do
       Call check_id(ids(), pasid)
       Call book_seat(bookings(), pasid, seatnr, ids())
       Call display_results(flightnr, pasid, seatnr)
       'Ask user if exit is required
       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 number
ids(0) = "WallaceW48P01"
ids(1) = "LauderH468P02"
ids(2) = "CarnegieA468P3"
ids(3) = "ScottW468P04"
ids(4) = "DeBruisR468P05"
flightnr = "HA468"
End Sub

Private Sub check_id(ByRef ids(), pasid)
'Declare variables
Dim found As Boolean
Dim counter As Integer
Dim counter2 As Integer
   Do
   'Prompt user for input
   pasid = InputBox("Enter your user ID")

   'Check if the ID is valid
   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())
Dim found As Boolean

  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
   If bookings(seatnr) = "" Then
           found = True
           bookings(seatnr) = "taken"
           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
   Loop Until found = True
End Sub



Private Sub display_results(ByVal flightnr, pasid, seatnr)
Dim pasid2 As String
'String concantination
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
txtbox.AddItem ""
End Sub



Oh and VB6 Professional.
User is offlineProfile CardPM
+Quote Post

Mach1Guy
RE: 2-D Arrays?
15 Dec, 2006 - 08:01 PM
Post #7

D.I.C Head
Group Icon

Joined: 4 Dec, 2006
Posts: 79



Thanked: 4 times
Dream Kudos: 25
My Contributions
hmm, I used to know VB6 but have long forgotten the differences compared to .NET so all the code I have posted may not have worked exactly as you would have liked.

As for the code below. First you prompt for the ID. the the logic goes into a loop, only exiting when found = True. Once you go inside the loop there is no prompt to enter another ID (pasid) therefore; it keeps looping with the same ID because you are never prompted for anther ID. You need to put the InputBox method right beneath the Do statement, so that the user can input a new ID if the previous one fails.
CODE

'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




CODE

       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

Also, wouldnt it be a little cleaner if you modified the above to this
CODE

       If ids(counter) = pasid Then
          found = True
          MsgBox ("The ID you was accepted")
       Else
          MsgBox ("The ID you entered was invalid")
       End If

counter2 gets declared but never used in check_id

This post has been edited by Mach1Guy: 15 Dec, 2006 - 08:08 PM
User is offlineProfile CardPM
+Quote Post

Mach1Guy
RE: 2-D Arrays?
15 Dec, 2006 - 08:24 PM
Post #8

D.I.C Head
Group Icon

Joined: 4 Dec, 2006
Posts: 79



Thanked: 4 times
Dream Kudos: 25
My Contributions
A possible solution could be as shown below. Basically instead of storing true or false in the bookings array, when it gets booked enter the ID (pasID) instead of true. and then when trying to book a seat check the array against the current pasid.

Basically I created a loop to go through the bookings array and count the number of times the current ID appears. If it appears even once then they have already booked a seat. The If statement has been modified to only book a seat if the bookings(seatnbr) = "" and the pasid does not have a booked seat.

Be very careful of my syntax. I didn't check over it and I have not used VB6 for a long time. But you should be able to get the idea of my code and fix my mistakes.
CODE

Private Sub book_seat(ByRef bookings(), ByVal pasid, ByRef seatnr, ids())
Dim found As Boolean

  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++
      End If
   Next
   If bookings(seatnr) = "" AND seatsBooked = 0 Then 'you could modify the = 0 part if you want them to say, be able to book 2 seats the you would say seatsBook <= 1
           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
   Loop Until found = True
End Sub


Well let me know what you think.

This post has been edited by Mach1Guy: 15 Dec, 2006 - 08:26 PM
User is offlineProfile CardPM
+Quote Post

Dalit
RE: 2-D Arrays?
16 Dec, 2006 - 02:40 AM
Post #9

D.I.C Head
**

Joined: 24 Jan, 2006
Posts: 54


My Contributions
Hmm, well I didnt take anything from post #7 because it doesn't get stuck in an infinite loop. If the user enters the wrong ID it does prompt for another one. Only problem is that if they hit "cancel" it doesn't exit the program instead it just prompts for another one

And changing the code to how you suggested changes it into an infinite loop that says "you have entered a invalid id"

counter2 was my attempt at the second array but I deleted that
---
From post #7. Thanks for the code, useful. However, I'm going to try to modify it so that they can change their seat number. Instead of just telling them that they've already booked a seat and and need to enter a different ID to continue.

This post has been edited by Dalit: 16 Dec, 2006 - 02:41 AM
User is offlineProfile CardPM
+Quote Post

Dalit
RE: 2-D Arrays?
16 Dec, 2006 - 03:41 AM
Post #10

D.I.C Head
**

Joined: 24 Jan, 2006
Posts: 54


My Contributions
Here's what i've got so far, problem is no matter if they clicked Yes or No they both exit the program.
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
   If seatsBooked = 0 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
         MsgBox ("A seat has already been booked for your UserID: " & pasid)
         answer = MsgBox("Do you want to change your booked seat?", vbYesNo, "Exit?")
            If responce = vbYes Then
                Call book_seat(bookings(), pasid, seatnr, ids())
            Else
                End
            End If
    End If
  Loop Until found = True
End Sub


This post has been edited by Dalit: 16 Dec, 2006 - 03:47 AM
User is offlineProfile CardPM
+Quote Post

Mach1Guy
RE: 2-D Arrays?
16 Dec, 2006 - 06:09 AM
Post #11

D.I.C Head
Group Icon

Joined: 4 Dec, 2006
Posts: 79



Thanked: 4 times
Dream Kudos: 25
My Contributions
weird that yours doesn't get stuck in an infinite loop on that part. must have something to do with the different versions because when i copied and pasted all the code into VB.NET and tried to run it I ran into an infinite loop.
User is offlineProfile CardPM
+Quote Post

Dalit
RE: 2-D Arrays?
16 Dec, 2006 - 06:17 AM
Post #12

D.I.C Head
**

Joined: 24 Jan, 2006
Posts: 54


My Contributions
It still gets stuck in the infinite loop, but only in the sense that when you click "Cancel" it doesnt actually exit and just reprompts the user for an input.

Coding so far

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


This post has been edited by Dalit: 16 Dec, 2006 - 06:18 AM
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 06:58PM

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