|
The following program does a binary search for a zip code from a text file. When the search is performed, then first record that is found, is returned, but i need the search to continue to find more of the same zip code that was entered. If and where do i put the loop to do so? THanks in advance.
Jim
Option Explicit
Dim zip() As String
Dim city() As String
Dim county() As String
Dim state() As String
Dim intnum As Double
Private Sub cmdsearchzip_Click()
Dim searchzip As String, result As Double
Label2.Caption = result
Call getinfo(searchzip)
Call findinfo(searchzip, result)
If result > 0 Then
Call showdata(result)
Else
lbloutput.Caption = "Zip not found!!"
End If
End Sub
'Private Sub findinfo(searchzip as String nt as String)
Private Sub Form_Load()
Dim i As Double
Dim zip1 As String
Dim city1 As String
Dim county1 As String
Dim state1 As String
Open "/forum/ctempZipCodes.txt" For Input As #1
Do While Not EOF(1)
Input #1, zip1, city1, county1, state1
intnum = intnum + 1
Loop
Close #1
ReDim zip(1 To intnum) As String, city(1 To intnum) As String
ReDim county(1 To intnum) As String, state(1 To intnum) As String
Open "/forum/ctempZipCodes.txt" For Input As #1
For i = 1 To intnum
Input #1, zip(i), city(i), county(i), state(i)
Next i
Close #1
End Sub
Private Sub txtcity_GotFocus()
cmdsearchzip.Visible = False
End Sub
Private Sub findinfo(searchzip As String, result As Double)
Dim foundflag As Boolean
Dim first As Double, middle As Double, last As Double
foundflag = False
first = 1
last = intnum
Do While (first <= last) And (Not foundflag)
middle = Int((first + last) / 2)
Label4.Caption = middle
Select Case zip(middle)
Case searchzip
foundflag = True
Case Is > searchzip
last = middle - 1
Case Is < searchzip
first = middle + 1
End Select
Loop
If foundflag Then
result = middle
Else
result = 0
End If
End Sub
Private Sub getinfo(searchzip As String)
searchzip = txtzip.Text
End Sub
Private Sub showdata(result As Double)
picout.Print "City(s) "; city(result)
picout.Print "County(s): " & county(result)
picout.Print "State(s): " & state(result)
End Sub
|
|
|
2 things are keeping you from seeing all the matches.
1st - You are only finding the first occurrance of the zip becuase of the Do While (first <= last) And (Not foundflag) Take this out and you will search thru the whole thing.
2nd - You only output at the very end of your program. Move the output to when you get a hit (the case statement), then you will see the multiple hits. If you cannot do this, then store the hits in an result() array, then loop thru that array at the end displaying the hits.
|
|
|
removing the not found falg did not work, but it created an ifinite loop. :o) I figured out how to display more than one zip by taking the first zip i find and comparing it the the next zip i search for and that worked just fine. Now i need to figure out how to do a shell sort on a 2 dimensional array. I got a book in front of me that tells me how to do a shell sort on a single dimension array.
<Added>
*Infinite*
|
|
|
DOH! Sorry... That makes sense... when you hit a zip that matches, you need to change first and last to avoid the infinite loop.
I've always been good at infinite loops.
|
|
|
|
|
|
|
// |