Qbasic, Quickbasic/Search problem

Advertisement


Question
Hi Alex,
I'm doing a library catalog program for my school project. I'm having problem in the Search() SUB procedure, although I didn't find any faults. And it's also gotten too complicated. Can you help me find my mistake or suggest a better way? Thanks a lot!

Regards,
Shashank

DECLARE SUB CENTER (ST$)
DECLARE SUB MENU ()
DECLARE SUB ADD ()
DECLARE SUB MODIFY ()
DECLARE SUB DEL ()
DECLARE SUB SORT ()
DECLARE SUB SEARCH ()
DECLARE SUB DISPLAYALL ()
DECLARE SUB TOTALD ()
DECLARE SUB LIBDATA ()
DECLARE SUB STATCHECK ()
CLS
DIM SHARED TOTAL, BORROW, SEARCHRES$
CALL STATCHECK
CALL MENU
END

SUB ADD
CLS
OPEN "LIBRARY.TXT" FOR APPEND AS #15
DO
PRINT
PRINT "------------------------------------------------------------------------------"
PRINT
INPUT "Enter Book Name:", BN$
INPUT "Enter Author's Name:", AU$
INPUT "Enter Pagination:", PAGE
INPUT "Enter Genre:", GENRE$
INPUT "Enter Price in Rs. "; PRICE
INPUT "Enter ISBN Number:", ISBN$
AUTHOR$ = RIGHT$(RTRIM$(AU$), 3)
GEN$ = LEFT$(LTRIM$(GENRE$), 2)
FILEID$ = AUTHOR$ + "/" + GEN$ + "-" + STR$(TOTAL)
TOTAL = TOTAL + 1
KILL "COUNT.TXT"
OPEN "COUNT.TXT" FOR OUTPUT AS #2
WRITE #2, TOTAL, BORROW
CLOSE #2
PRINT "File ID automatically assigned to Book:", FILEID$
WRITE #15, BN$, AU$, PAGE, GENRE$, PRICE, ISBN$, FILEID$, TOTAL
INPUT "Do you want to add any more records?(Y/N)", IP$
LOOP WHILE LEFT$(UCASE$(IP$), 1) = "Y"
CLOSE #15
DO: LOOP WHILE INKEY$ = ""
CALL MENU
END SUB

SUB CENTER (ST$)
LENGTH = LEN(ST$)
TABN = (80 - LENGTH) / 2
PRINT TAB(TABN); ST$
END SUB

SUB DEL
END SUB

SUB DISPLAYALL
END SUB

SUB LIBDATA
CLS
CALL CENTER("Library Statistics")
PRINT "Total No. Of Books:", TOTAL
PRINT "No. of Borrowed Books:", BORROW
PRINT "Remaining Books In Stock:", TOTAL - BORROW
DO: LOOP WHILE INKEY$ = ""
CALL MENU
END SUB

SUB MENU
CLS
CALL CENTER("Library Catalog Software")
PRINT
CALL CENTER("Main Menu")
PRINT
PRINT : PRINT
PRINT "1.   Add New Records"
PRINT "2.   Modify Existing Records"
PRINT "3.   Delete Records"
PRINT "4.   Search Existing Records"
PRINT "5.   Sort Records"
PRINT "6.   Display All Records"
PRINT "7.   Display Library Statistics"
PRINT "8.   Exit Program"
CHOICE:
PRINT
INPUT "Enter your choice <1/2/3/4/5/6/7/8>:", CH%
SELECT CASE CH%
CASE 1
CALL ADD
CASE 2
CALL MODIFY
CASE 3
CALL DEL
CASE 4
CALL SEARCH
CASE 5
CALL SORT
CASE 6
CALL DISPLAYALL
CASE 7
CALL LIBDATA
END SELECT
IF CH% > 8 OR CH% = 0 THEN
BEEP
PRINT "INVALID CHOICE"
GOTO CHOICE
END IF
END SUB

SUB MODIFY
PRINT "Enter string to search for modifying file:", SR$
CALL SEARCH
END SUB

SUB SEARCH
INPUT "Enter string to search:", SS$
SS$ = UCASE$(SS$)
OPEN "LIBRARY.TXT" FOR INPUT AS #1
DO WHILE NOT EOF(1)
INPUT #1, BN$, AU$, PAGE, GENRE$, PRICE, ISBN$, FILEID$, TOTAL
FOR I = 1 TO 8
SELECT CASE I
CASE 1
BOOK$ = BN$
CASE 2
BOOK$ = AU$
CASE 3
BOOK$ = STR$(PAGE)
CASE 4
BOOK$ = GENRE$
CASE 5
BOOK$ = STR$(PRICE)
CASE 6
BOOK$ = ISBN$
CASE 7
BOOK$ = FILEID$
CASE 8
BOOK$ = STR$(TOTAL)
END SELECT
C$ = UCASE$(LTRIM$(RTRIM$(BOOK$)))
START = 1
FOR M = START TO LEN(C$)
P$ = MID$(C$, M, 1)
IF P$ = " " THEN
COMPR$ = LTRIM$(RTRIM$(MID$(C$, START, M)))
START = M + 1
IF LTRIM$(RTRIM$(COMPR$)) = LTRIM$(RTRIM$(SS$)) THEN
 PRINT "------------------------------------------------------------------------------"
PRINT BN$, AU$, PAGE, GENRE$, PRICE, ISBN$, FILEID$, TOTAL
PRINT "------------------------------------------------------------------------------"
GOTO NEW
END IF
END IF
NEXT M
NEW:
NEXT I
LOOP
CLOSE #1
END SUB
     

SUB SORT
END SUB

SUB STATCHECK
I = 0
OPEN "COUNT.TXT" FOR APPEND AS #1
WRITE #1, 0, 0
CLOSE #1
OPEN "COUNT.TXT" FOR INPUT AS #1
DO WHILE NOT EOF(1)
INPUT #1, N, NO
I = I + 1
LOOP
CLOSE #1
IF I <> 1 THEN
OPEN "COUNT.TXT" FOR INPUT AS #2
FOR M = 1 TO I - 1
INPUT #2, TOTAL, BORROW
NEXT M
END IF
CLOSE #2
END SUB

SUB TOTALD
END SUB  

Answer
Hi, Shane,

I think this is the problem with your code:

FOR M = START TO LEN(C$)
P$ = MID$(C$, M, 1)
IF P$ = " " THEN
COMPR$ = LTRIM$(RTRIM$(MID$(C$, START, M)))
START = M + 1
IF LTRIM$(RTRIM$(COMPR$)) = LTRIM$(RTRIM$(SS$)) THEN
PRINT "------------------------------------------------------------------------------"
PRINT BN$, AU$, PAGE, GENRE$, PRICE, ISBN$, FILEID$, TOTAL
PRINT "------------------------------------------------------------------------------"
GOTO NEW
END IF
END IF
NEXT M


Specifically this line:

COMPR$ = LTRIM$( RTRIM$( MID$( C$, START, M ) ) )

The MID$() statement works like this
MID$( string, start-position, length )
It looks like you are using it like:
MID$( string, start-position, end-position )

To fix that, do this:

MID$( C$, START, M - START + 1 )

I hope that solves the problem, if not, just fire off another question,
-Alex

Qbasic, Quickbasic

All Answers


Answers by Expert:


Ask Experts

Volunteer


Alex Barry

Expertise

I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, QB64, c/c++, python, lua, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.

Experience

I have been programming in *Basic dialects since 2000, as mentioned in my expertise. After a year of QBasic, I learned C and C++, and dabbled a little in ASM (I don't program in ASM - I literally just played around to see how things work). When QB64 and FreeBASIC were released, I played with those languages. At the time, FreeBASIC offered more functionality and I sided with that language for a while. During that time, while I was learning new languages, that I would see what scripting languages are available, where I took up python and lua. I started to notice a staleness to QB64's development (which I kept tabs on from time to time), and am now trying to be active in it's community and maybe in it's development in the future. Currently, I am only active on the QB64.net forums, but I appear on occasion on FreeBASIC.net's forums as well.

Education/Credentials
Highschool - 2007

©2012 About.com, a part of The New York Times Company. All rights reserved.