13 Replies - 1970 Views - Last Post: 16 February 2007 - 03:59 PM Rate Topic: -----

#1 Dalit  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 24-January 06

2-D Arrays?

Posted 12 December 2006 - 01:38 PM

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)

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


Is This A Good Question/Topic? 0
  • +

Replies To: 2-D Arrays?

#2 Dalit  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 24-January 06

Re: 2-D Arrays?

Posted 13 December 2006 - 08:44 AM

Here's a bump and my attempt at the 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

Was This Post Helpful? 0
  • +
  • -

#3 Mach1Guy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 04-December 06

Re: 2-D Arrays?

Posted 15 December 2006 - 07:22 AM

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
Was This Post Helpful? 0
  • +
  • -

#4 Dalit  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 24-January 06

Re: 2-D Arrays?

Posted 15 December 2006 - 02:37 PM

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
Was This Post Helpful? 0
  • +
  • -

#5 Mach1Guy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 04-December 06

Re: 2-D Arrays?

Posted 15 December 2006 - 03:43 PM

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.
		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 December 2006 - 03:44 PM

Was This Post Helpful? 0
  • +
  • -

#6 Dalit  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 24-January 06

Re: 2-D Arrays?

Posted 15 December 2006 - 07:19 PM

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

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.
Was This Post Helpful? 0
  • +
  • -

#7 Mach1Guy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 04-December 06

Re: 2-D Arrays?

Posted 15 December 2006 - 09:01 PM

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.
'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





	   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
	   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 December 2006 - 09:08 PM

Was This Post Helpful? 0
  • +
  • -

#8 Mach1Guy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 04-December 06

Re: 2-D Arrays?

Posted 15 December 2006 - 09:24 PM

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.
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 December 2006 - 09:26 PM

Was This Post Helpful? 0
  • +
  • -

#9 Dalit  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 24-January 06

Re: 2-D Arrays?

Posted 16 December 2006 - 03:40 AM

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 December 2006 - 03:41 AM

Was This Post Helpful? 0
  • +
  • -

#10 Dalit  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 24-January 06

Re: 2-D Arrays?

Posted 16 December 2006 - 04:41 AM

Here's what i've got so far, problem is no matter if they clicked Yes or No they both exit the program.
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 December 2006 - 04:47 AM

Was This Post Helpful? 0
  • +
  • -

#11 Mach1Guy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 04-December 06

Re: 2-D Arrays?

Posted 16 December 2006 - 07:09 AM

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.
Was This Post Helpful? 0
  • +
  • -

#12 Dalit  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 24-January 06

Re: 2-D Arrays?

Posted 16 December 2006 - 07:17 AM

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

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 December 2006 - 07:18 AM

Was This Post Helpful? 0
  • +
  • -

#13 iamsteve  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-January 07

Re: 2-D Arrays?

Posted 29 January 2007 - 01:08 PM

View PostDalit, on 16 Dec, 2006 - 07:17 AM, said:

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

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








het dalit just wanderin if u got that program working if so any chance of help thanks
Was This Post Helpful? 0
  • +
  • -

#14 KKKKK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 16-February 07

Re: 2-D Arrays?

Posted 16 February 2007 - 03:59 PM

Yeah, I'm doing this as an assessment and am finding it really hard too. Could you post the finished code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1