AllExperts > Qbasic, Quickbasic 
Search      
Qbasic, Quickbasic
Volunteer
Answers to thousands of questions
 Home · More Qbasic, Quickbasic Questions · Answer Library  · Encyclopedia ·
More Qbasic, Quickbasic Answers
Question Library

Ask a question about Qbasic, Quickbasic
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About 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, c/c++, python, 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
Hobby programming since 2000

I no longer belong to any community programming groups, but do have knowledge of *basic dialects and C/C++

 
   

You are here:  Experts > Computing/Technology > Basic > Qbasic, Quickbasic > Search problem

Qbasic, Quickbasic - Search problem


Expert: Alex Barry - 10/18/2009

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

View Follow-Ups    Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.