Please use this Board for QBasic related requests ( file research, programming, etc.)
Moderators:Administrator, Global Moderator
-
Zymatic
- Newbie
- Posts:6
- Joined:Wed Apr 02, 2003 8:32 am
help with searching and files
Post
by Zymatic » Thu Apr 10, 2003 8:17 am
I am writing a program for computer science class and the guide lines require it have different features. I alreayd posted about help on the sorting part of it now I need help with searching the database created by the program. Everything is stored in random files. I don't have the code from the actual program to include but I wrote a quick test snippit that shows the basic idea of how it is coded in my program. I will try to get a snippit of the ral code to further help you with helping me. The problem I'm having is when it compares the different variables for the search it doesn't seem to find that they are the same. when I printed out the variables seperately the were infact the same but the program just prints out my ELSE part or my IF statement. any help or comments or suggestions would be greatly appricated! Code is below. ( none of it is commented or even tabbed in or spaced correctly because it is just a quick bit I wrote for this post)
Code: Select all
TYPE testRecord
test AS STRING * 20
END TYPE
DIM test AS testRecord
DIM test2$(1 TO 4)
OPEN "test.dat" FOR RANDOM AS #1 LEN = LEN(test)
FOR x = 1 TO 4
READ test.test
PUT #1, x, test
NEXT x
FOR y = 1 TO 4
GET #1, x, test
test2$(x) = test.test
NEXT y
INPUT "search for"; target$
FOR y = 1 TO 4
IF target$ = test2$(y) THEN
PRINT target$
ELSE
END IF
NEXT y
FOR y = 1 TO 4
PRINT test2$(y)
NEXT y
DATA aaaa, bbbb, cccc, dddd
CLOSE
END
thanks,
Zymatic
-
Dr_Davenstein
- QBasic God
- Posts:166
- Joined:Tue Mar 25, 2003 12:45 am
- Location:U.S.A.
-
Contact:
Post
by Dr_Davenstein » Sat Apr 12, 2003 8:19 am
I noticed that you used the syntax "STRING * 20".
You might try to incorporate Rtrim$ into your code or you might try to use the exact same TYPE definition for both variables.
TYPE testRecord
test AS STRING * 20
END TYPE
DIM test AS testRecord
'DIM test2$(1 TO 4)
Instead of that...
:' Try this for instance.
DIM test2$(1 TO 4) as testrecord
Come check out [url=http://www.freebasic.net/forum/index.php]FreeBASIC[/url]. The syntax is based on QuickBasic, but it expands to use pointers, operator overloading, etc... The list goes on and on!
-
Zymatic
- Newbie
- Posts:6
- Joined:Wed Apr 02, 2003 8:32 am
Post
by Zymatic » Sat Apr 12, 2003 8:22 am
OK, thanks for your suggestion, I'll try changing the DIM statement and see if it helps! I'm not familiar with the Rtrim$ statement, could you explain it to me, or atleast give a brief description of what it does? Thanks for your help!
Zymatic
-
Zymatic
- Newbie
- Posts:6
- Joined:Wed Apr 02, 2003 8:32 am
Post
by Zymatic » Tue Apr 15, 2003 4:23 am
Still having trouble with this little problem, and since the code I posted wasn't code from my actual program and it would be harder for you guys to help me out completely I've decided to upload my whole program .bas file. if you could take the time to look at it and help that'd be great! program written in qbasic 1.1.
http://www.angelfire.com/ex/tempsite/ch10test.bas
thanks
Zymatic
-
Baz
- Jr. Member
- Posts:11
- Joined:Mon Mar 10, 2003 9:55 pm
- Location:Suffolk UK
Post
by Baz » Tue Apr 15, 2003 4:35 am
HI
Sorry that link does not work for me :-/
-
Guest
Post
by Guest » Tue Apr 15, 2003 12:35 pm
'man what a mess!!
'here's the fixed up version.
'You should realy post your code...no
'telling what else is could be a problem.
'--MiggyD (@ qbcafe')
'***NOTE your problem code is REM'd out
'TYPE testRecord
'test AS STRING * 20
'END TYPE
'--fixed--not proper programming:unintelligible variables
TYPE RecordStructure
SomeStr AS STRING * 20
END TYPE
'DIM test AS testRecord
'DIM test2$(1 TO 4)
'--fixed--unintelligible variables
DIM MagicWord AS RecordStructure
DIM ComparisonArray$(1 TO 4)
'OPEN "test.dat" FOR RANDOM AS #1 LEN = LEN(test)
'--fixed--pretty much OK but I've changed the ARRAY's Name
OPEN "Test.DAT" FOR RANDOM AS #1 LEN = LEN(MagicWord)
'FOR x = 1 TO 4
'READ test.test
'PUT #1, x, test
'NEXT x
'--fixed--What's up with the X in 'PUT #1' ?
FOR x = 1 TO 4
READ MagicWord.SomeStr
PUT #1, , MagicWord.SomeStr
NEXT x
'--fixed--you didn't reset the pointer
SEEK #1, 1
'FOR Y = 1 TO 4
'GET #1, x, test
'test2$(x) = test.test
'NEXT Y
'--fixed--You are using 'Y' in the FOR/NEXT, yet
' storing with an 'X'; what's up with the 'X'
' in 'GET #1' ?
FOR Y = 1 TO 4
GET #1, , MagicWord.SomeStr
ComparisonArray$(Y) = RTRIM$(MagicWord.SomeStr)
NEXT Y
'--fixed--moved CLOSE to avoid thrashing of disk; also
' we no longer need the file opened (for the rest
' of the program.)
CLOSE
'INPUT "search for"; target$
'--fixed--added CLS; also changed INPUT prompt so
' users know what data is requested.
CLS
INPUT "Enter String To Search For: "; target$
'FOR Y = 1 TO 4
' IF target$ = test2$(Y) THEN
' PRINT target$
' ELSE
' END IF
' NEXT Y
'--fixed--Changed due to ARRAY re-naming; also
' better output of information and data
FOR Y = 1 TO 4
IF target$ = ComparisonArray$(Y) THEN
PRINT
PRINT "Match Found in #"; LTRIM$(STR$(Y))
PRINT "Target: "; target$
PRINT "Array : "; ComparisonArray$(Y)
PRINT
END IF
NEXT Y
'FOR Y = 1 TO 4
'PRINT test2$(Y)
'NEXT Y
'--fixed--due to ARRAY re-naming
FOR Y = 1 TO 4
PRINT ComparisonArray$(Y)
NEXT Y
'--not fixed--this is fine the way it is
DATA aaaa, bbbb, cccc, dddd
'--fixed--moved CLOSE to avoid thrashing of disk;
' see above.
'CLOSE
'--not fixed--this is fine the way it is
END
-----------------------------
Three of your major problems with this was:
you used 'X' to PUT and GET.
you used 'Y' to GET (x).
you didn't reset file pointer to beginning of file.
Let us know if this has helped or not. ALSO, you should really post all your code incase there are any other potential problems that you have not come across yet.
--MiggyD
-
Guest
Post
by Guest » Tue Apr 15, 2003 12:48 pm
Sorry about the "what a mess" statement, I just re-read your first post (carefully this time) and you did state that it was not the
actual code but a facsimile. You should throw eggs on my face.
The reason for this second post was to amend one of the "--fixed--" notes I mentioned above. In particulare:
Code: Select all
'FOR Y = 1 TO 4
' IF target$ = test2$(Y) THEN
' PRINT target$
' ELSE
' END IF
' NEXT Y
'--fixed--Changed due to ARRAY re-naming; also
' better output of information and data
FOR Y = 1 TO 4
IF target$ = ComparisonArray$(Y) THEN
PRINT
PRINT "Match Found in #"; LTRIM$(STR$(Y))
PRINT "Target: "; target$
PRINT "Array : "; ComparisonArray$(Y)
PRINT
END IF
NEXT Y
It should have been:
'--fixed--Changed due to ARRAY re-naming; better
' output of information and data;
also nothing to
' do in the ELSE section so it was removed.
--MiggyD
-
Zymatic
- Newbie
- Posts:6
- Joined:Wed Apr 02, 2003 8:32 am
Post
by Zymatic » Wed Apr 16, 2003 12:56 am
I don't mind that you read it wrong and posted that stuff mistakes happen. anyway the reason why I didn't post my actual program code is it is quite long and I just figured it would be easier to allow you guys to download my .bas file and look at it that way so you can see all the different parts.
also you mentioned about proper programming and resetting pointers and such, if things like that are in my programming it is because I am just doing it the way my highschool computer science teacher taught it/wants it. and the pointers thing he never even taught us about those so thats why I didn't use it. I'll try to fix the link and if that doesn't work I'll post just my search section from my program.
Zymatic
-
Zymatic
- Newbie
- Posts:6
- Joined:Wed Apr 02, 2003 8:32 am
Post
by Zymatic » Wed Apr 16, 2003 1:09 am
ok so I can't get the link to work so here is the code for my sequential search. you'll probably find a bunch of improper programming techniques but I'm not as concerned with that (unless it fixes my problem) as i am with trying to get the search to work! alot of things are REM'd out as well becaue they were stuff I added to see if the variables actually had data etc.. or just me trying different methods of getting it to work.
Code: Select all
SUB bandsearchseq (cd AS CDRecord, band$(), title$(), year(), genre$())
'*** this sub uses a sequential to search for a user specified band ***
CLS
'*** asks for band name ot search for ***
INPUT "Enter band name to search for"; target$
target$ = UCASE$(target$)
' ***** puts info from file into arrays ******
FOR a = 1 TO LOF(1) / LEN(cd)
GET #1, a, cd
band$(a) = cd.band
title$(a) = cd.title
year(a) = cd.year
genre$(a) = cd.genre
NEXT a
' *** searches each record and if target is found outputs it ***
FOR a = 1 TO LOF(1) / LEN(cd)
PRINT band$(a)
IF target$ <> band$(a) THEN
PRINT "Band not found. Please try again."
ELSE
PRINT
PRINT " Band Name: "; band$(a)
PRINT " Cd Title: "; title$(a)
PRINT " year: "; year(a)
PRINT " genre: "; genre$(a)
END IF
NEXT a
b = 1
DO WHILE b = 1
INPUT "Return to main menu (y/n)"; choice$
choice$ = UCASE$(choice$) ' make choice$ upper case
IF choice$ = "Y" THEN ' if choice is Y then
b = 0 ' call menu sub
CALL menu(cd, band$(), title$(), year(), genre$())
ELSEIF choice$ = "N" THEN
b = 0
PRINT "Thank You. GoodBye." ' else end program
END
ELSEIF choice$ <> "Y" AND choice$ <> "N" THEN
PRINT "Invalid choice"
b = 1
END IF
LOOP
END SUB
thanks again for any help!
Zymatic
-
John_Schofield
- Full Member
- Posts:36
- Joined:Tue Mar 18, 2003 3:04 am
- Location:Lancashire, UK
-
Contact:
Post
by John_Schofield » Thu Apr 17, 2003 4:14 am
Hi
What I think I'd do here (without wanting to change your code too much) would be as follows:
1) ' ***** puts info from file into arrays ******
I'd ignore this bit of code until you find the right record - you're just slowing down the searching.
2) foundrec%=0: rem set marker for found record
FOR a = 1 TO LOF(1) / LEN(cd)
GET 1, a
if ucase$(ltrim$(rtrim$(target$)))=ucase$(ltrim$(rtrim$(band$))) then
rem - this strips off all spaces both at the start and at the end of the two strings and makes sure you're checking same case state
rem load the elements from the file as record found
foundrec%=1:exit for
NEXT a
if foundrec%=0 then
rem do not found the record routine
else
rem do found record routine (load elements and display)
....rest of program
What you should also consider is using the instr function so that you can search on part of a band's name.
You seem to be mixing up different types of variables which is probably confusing your program. Try just sticking to strings for the random file where you can and this means you can just do string compares until you get the right answer. The idea of using ltrim$ and rtrim$ means that only the relevant part of the string (i.e. the actual name) is checked
There's still some further work to be done here and I'll look at it some more if you like when I have time.
regards
John
-
John_Schofield
- Full Member
- Posts:36
- Joined:Tue Mar 18, 2003 3:04 am
- Location:Lancashire, UK
-
Contact:
Post
by John_Schofield » Thu Apr 17, 2003 4:17 am
previous reply was a bit messy on the loops because of the tiny window for the reply - I lost track of where I was. Of course, there should be an END IF statement atfter the "foundrec%=1:exit for" line
-
Guest
Post
by Guest » Thu Apr 17, 2003 7:05 am
Are you sure you can't paste the entire code? Reason is that this Sub alone doesn't help. We need to see the global variables from the main mod and how the CALLing is being done.
Right off the top, I see a (potentially) major problem with recursing in your last DO/LOOP--if it hasn't happened yet. You'll probably get it to work 15 or 16 times before the program crashes.
I'll check your code
later, but it seems to need some VERY major changes. And, since you can't paste the entire code, I'll have to make some presumptions and that's not good.
In the meantime, you can check out the following which is a modification to the one I posted on Apr 15. I've add the SUBs so that you can compare what you have to how it might be in order for your code to function properly.
Code: Select all
'--This is in main module
'--you can better see this when you use
'--QB to view it.
'--MiggyD
'
TYPE RecordStructure
SomeStr AS STRING * 20
SomeTime AS DOUBLE
END TYPE
DECLARE SUB SaveArray ()
DECLARE SUB ReadArray ()
DIM SHARED MagicWord AS RecordStructure
DIM SHARED ComparisonArray(1 TO 4) AS RecordStructure
'DIM ComparisonArray$(1 TO 4)
OPEN "Test.tmp" FOR RANDOM AS #1 LEN = LEN(MagicWord)
CLS
PRINT "Saving data in 5 seconds...please wait"
CALL SaveArray
PRINT "Recording Data into an Array...please wait"
SLEEP 3
CALL ReadArray
CLOSE 'avoid thrashing
INPUT "Enter String To Search For: "; Target$
FOR Y = 1 TO 4
IF Target$ = RTRIM$(ComparisonArray(Y).SomeStr) THEN
PRINT
PRINT "Match Found in #"; LTRIM$(STR$(Y))
PRINT "Target: "; Target$
'PRINT "Array : "; ComparisonArray$(Y)
PRINT "SomeStr:"; ComparisonArray(Y).SomeStr
PRINT "SomeTime:"; ComparisonArray(Y).SomeTime
PRINT
END IF
NEXT Y
FOR Y = 1 TO 4
'PRINT ComparisonArray$(Y)
PRINT ComparisonArray(Y).SomeStr, STR$(ComparisonArray(Y).SomeTime)
NEXT Y
SLEEP 7
PRINT
INPUT "Want to see the actual data that is stored? (N/y)"; Target$
IF UCASE$(Target$) = "Y" THEN SHELL "TYPE Test.tmp"
DATA aaaa, bbbb, cccc, dddd
END
SUB ReadArray
'--File is already opened from main module; therefore
'--I need to tell the computer where the next READ FROM
'--FILE position is supposed to be by using SEEK.
SEEK #1, 1
FOR Y = 1 TO 4
GET #1, , MagicWord
ComparisonArray(Y) = MagicWord
'ComparisonArray$(Y) = RTRIM$(MagicWord.SomeStr)
NEXT Y
END SUB
SUB SaveArray
FOR x = 1 TO 4
READ MagicWord.SomeStr
MagicWord.SomeTime = TIMER
PUT #1, , MagicWord
SLEEP 1 'to show MW.SomeTime is saving properly
NEXT x
END SUB
HTH in the meantime.
--MiggyD
-
Guest
Post
by Guest » Thu Apr 17, 2003 1:18 pm
It took me a while to fix it up so that I could even begin to use it and determin
which variables you said you created for debugging purposes 'cause you didn't mention which ones and get rid of them. :'(
Anyway, here's my fix for your "sub"
Code: Select all
'-- I had to create some code in the
' Main Module for testing purposes.
' Therefore, I've not included them
' in this post. -- MiggyD @ QBCaf‚ Forum
DECLARE SUB BandSearchSeq ()
'$INCLUDE: 'MyTest.001'
SaveBandInfo
BandSearchSeq
CLOSE
END
'----------------------- End of Main Module -----------------------
SUB BandSearchSeq
'*************************************************
'*** Sequential search for user specified band ***
'*************************************************
'I will presume that you have the following in your Main Module:
'
' 1) a TYPE CDRecord coded.
' 2) a DIM SHARED CD AS CDRecord statement.
' 3) an OPEN filename FOR RANDOM AS 1 LEN = LEN(CDRecord) statment.
'
'--Local variable to count number of records that match.
Matches = 0
'--Get "BandName" from user to find
CLS
INPUT "Enter band name to search for"; Target$
Target$ = UCASE$(Target$)
'--Tell computer to start from the VERY beginning
'--of the OPENed file. ***Did you forget this?***
SEEK #1, 1
'--Sequential Search and Comparison Checking Routine/Code--
FOR RecNumber = 1 TO LOF(1) / LEN(CD)
'--Get 1 record from the file
GET #1, , CD
'--Compare Target$ to data just retrieved from file
IF Target$ = RTRIM$(UCASE$(CD.Band)) THEN
'--Do this ONLY if a match is found
PRINT
PRINT "Match Found at Record #"; STR$(RecNumber)
PRINT " Band Name: "; CD.Band
PRINT " CD's Title: "; CD.Title
PRINT "Year Released: "; CD.Year
PRINT "Style / Genre: "; CD.Genre
Matches = Matches + 1
END IF
NEXT RecNumber
'--Display number of matches
PRINT
PRINT "A total of"; STR$(Matches); " record(s) have been found."
PRINT
DO
'--NOTE: You can re-use Target$ because we are all done
'--using it for the SEARCHing task...so instead of eating
'--up valuable 64k of DOS memory we'll re-use Target$ for
'--this task instead of creating another variable called
'--[CHOICE$]. OK?
'
'INPUT "Return to main menu (y/n)"; Target$
INPUT "Search again? (y/n)"; Target$
Target$ = UCASE$(Target$) ' make choice$ upper case
'--Act on user's request
IF Target$ = "Y" THEN
'--User wants to continue, so exit DO/LOOP; and
'--eventually this SUB.
'
EXIT DO
ELSEIF Target$ = "N" THEN
'--User wants to terminate program, so do it.
'
PRINT "Thank You. GoodBye."
END
ELSE
'--Key pressed was NOT a "Y" (see IF above)!
'--Key pressed was NOT a "N" (see ELSEIF above)!
'--Key pressed was something "else" (get it? "ELSE"? hehe)
PRINT "Invalid Choice...Please re-enter": PRINT : SLEEP 2
END IF
LOOP WHILE 0 = 0
END SUB
'--------------------------- End of Sub ---------------------------
SUB SaveBandInfo
'$INCLUDE: 'MyTest.002'
END SUB
'--------------------------- End of Sub ---------------------------
Again, if you can, post the entire code for all to help you out.
--MiggyD