You are here:

Qbasic, Quickbasic/How to do a SEARCH among DATA file

Advertisement


Question
QUESTION: Hello,
I'm 54 years of age and, I don't remember some of the Quick BASIC commands for the program I'm trying to create.  Can you PLEASE suggest where my mistakes are and, modify them as much as possible; here's what I'm working on:

01 REM - SEARCHING OF 200 EMPLOYEE RECORDS
02 REM - Microsoft QuickBASIC
99  REM - DATA = Date Of Hire, Soc.Sec. #, Date Of Termiation
100 DATA 01-25-2001, 331-01-5981, 09-11-2011
200 DATA 03-30-1981, 053-00-9153, 01-20-2000
300 DATA 11-13-1983, 004-35-5391, 12-13-2009
.
. (total of 200 DATA of empoyees)
.
4000 DIM A(200,3)
5000 FOR I = 1 TO 200
6000 FOR J = 1 TO 3
7000 READ A (I,J)
7500 PRINT A (I,J),
8000 NEXT J
9000 NEXT I
10000 END

I'd lie to eliminate PRINT on line 7500 and, instead search the 200
employee records, based on Social Security Number; example:
7500 INPUT "Enter Soc. Sec. # to be searched?"; Z
and, if number entered is found in the 200 employee records,
to be shown on SCREEN, along with Date of Hire, S.S.#, Date of
Termination.
If the Soc.Sec.# INPUTed/entered is not found then,
7501 PRINT "No Such Record Found!"

Is this how the program should look like?. I don't think that variable "Z" would encompass just the Soc. Sec. #.

01 REM - SEARCHING OF 200 EMPLOYEE RECORDS
02 REM - based on S.S.# entered
03 REM
99  REM - Date Of Hire, Soc.Sec. #, Date Of Termiation
100 DATA 01-25-2001, 331-01-5981, 09-11-2011
200 DATA 03-30-1981, 053-00-9153, 01-20-2000
300 DATA 11-13-1983, 004-35-5391, 12-13-2009
.
. (total of 200 DATA of empoyees)
.
4000 DIM A(200,3)
5000 FOR I = 1 TO 200
6000 FOR J = 1 TO 3
7000 READ A (I,J)
7001 INPUT "Enter S.S.# to be searched?"; Z
7002 IF Z = A (J) THEN
7003 PRINT A (J)
7004 ELSE
7005 PRINT "NO such records found!"
8000 NEXT J
9000 NEXT I
10000 END

Any suggestions would be appreciated!.
Thank you,
Michael.

ANSWER: Hi Michael,

So the first issue I see is your data statements - the individual pieces of data need to be wrapped in quotes (since they contain both numbers and dashes, and can begin with a 0, a string is probably our best bet here).

Second issue, you should specify the data type for A on 4000.  In this case, since my above suggestion was to wrap the data in quotes, you should use a string.  The easiest way for that is changing the line to:

4000 DIM A$(200,3)

You will want to do the same for 7001:

7001 INPUT "Enter S.S.# to be searched?"; Z$


The next issue is your search in 7001 to 7005.  What's happening here is while you are loading data, you're forcing the user to do an input, even though a full record hasn't been loaded yet.  What you want to do is move that code either before the loop (if the program is only to do one query per run), or after in it's own loop (if the program is to make multiple queries on the data).

Presuming you are allowing for multiple queries, after 9000, I would recommend:

9001 DO
9002 INPUT "Enter S.S.# to be searched?"; Z$
9003 IF ( Z$ <> "" ) THEN
9004 FOR I% = 1 TO 200
9005 REM Using I, 2 because the second column seems to be where you're storing SS#s
9006 IF Z$ = A$(I, 2) THEN
9007 PRINT A$(I,1), A$(I, 2), A$(I,3)
9008 EXIT FOR
9009 END IF
9010 NEXT I
9011 END IF
9012 LOOP WHILE Z$ <> ""
10000 END


I'm not sure what version of BASIC you are using, but if it is Q/QuickBASIC, you may find this online version of the help useful for remembering commands.  :)

Take care and happy programming,
-Alex


---------- FOLLOW-UP ----------

QUESTION: Hello,
Since your last reply, I came up with an alternative program but, can't get it to END and, if the ss.# is NOT found, I'd like to get a prompt telling me so; as it's written right now, if I enter a S.S.# that's not in the DATA, it keeps on asking me for entering a s.s.#. Can you PLEASE, suggest where is/are my problems!.


1 CLS
200 DIM N$(4), B$(4), C$(4)
210 REM - READ Number Of Records:
220 READ K
230 REM - READ Records Into ARRAYS:
240  FOR I = 1 TO K
250   READ N$(I), B$(I), C$(I)
260  NEXT I
350 REM - Repeat
360 PRINT "Please, ENTER s.s.number to be SEARCHED in the Employee RECORDS ";
370 INPUT R$
380 REM - SEARCH For RECORD: S.S.Number ALREADY Chosen
390 FOR I = 1 TO K
400   IF R$ <> B$(I) THEN 481
401 PRINT
402 PRINT
403 PRINT
420 PRINT "S. S.Number you've entered was ALREADY on LIST since : "; N$(I), "S.S.Number was an EXACT MATCH:"; B$(I), "Date FIRED:"; C$(I)
430 PRINT
440 PRINT
450 PRINT "No Record Found!. Would you like to try again?. (Y/N)"; F
451  INPUT F
452  IF F = no THEN 480
453  IF F = yes THEN 500
480  END
481  NEXT I
500 GOTO 350
1000 REM - DATA:
1010 REM - Number Of Records:
1020 DATA 4
1030 REM - N$=Date HIRED, B$=Soc. Sec. Number, C$=Date FIRED
1040 DATA 01-25-2001, 331-01-5981, 09-11-2011
1050 DATA 03-30-1981, 053-00-9153, 01-20-2000
1060 DATA 11-13-1983, 004-35-5391, 12-13-2009
1070 DATA 10-29-2011, 365-76-1200, 10-30-2013
9999 END

If NO record of s.s.# is found, I want it to say so ("No record found...) and, to let me choose (Y/N) if I should try again or, END program.

If S.S.# is found on DATA file, it should state that an exact match was found, exhibit the s.s.#, date of hiring and, date of termination; I'd like to be asked if I choose (Y/N) to try again or, END program.
I just can't make it work.

Thanks,
Michael.

ANSWER: On line 450, are you expecting a string?  If so, it should be F$, not just F, and you will need 452 and 453 to be checking against strings like this:

452 IF SUBSTR$( UCASE$( F$ ), 1, 1 ) = "N" THEN GOTO 480
453 IF SUBSTR$( UCASE$( F$ ), 1, 1 ) = "Y" THEN GOTO 500


I'm wrapping F$ in UCASE$() to make sure your input is upper case, and substr because we know if the first letter is "Y" then the user is certainly saying "yes" and if it is "N" then the user is certainly saying "no".  I also noticed you forgot to write GOTO before those numbers after THEN, so I added that for you.

And it looks like your data still needs to be wrapped in quotes like this:

1040 DATA "01-25-2001", "331-01-5981", "09-11-2011"

You'll need to fix all of your data statements to be wrapped in quotes like that, too.

Hope that helps,
-Alex

---------- FOLLOW-UP ----------

QUESTION: Hi Alex,

I've tried to RUN it but, for lines 452 and 453, I'm getting a TYPE MISMATCH error message and, underlines only this word: SUBSTR$ from these two lines.

I just found out that "TYPE MISMATCH" error means that a numeric value is assigned to a string variable or, a string is assigned to a numeric variable.
**********************************************************
1 CLS
200 DIM N$(4), B$(4), C$(4)
210 REM - READ Number Of Records:
220 READ K
230 REM - READ Records Into ARRAYS:
240  FOR I = 1 TO K
250   READ N$(I), B$(I), C$(I)
260  NEXT I
350 REM - Repeat
360 PRINT "Please, ENTER s.s.number to be SEARCHED in the Employee RECORDS ";
370 INPUT R$
380 REM - SEARCH For RECORD: S.S.Number ALREADY Chosen
390 FOR I = 1 TO K
400   IF R$ <> B$(I) THEN 481
401 PRINT
402 PRINT
403 PRINT
420 PRINT "S. S.Number you've entered was ALREADY on LIST since : "; N$(I), "S.S.Number was an EXACT MATCH:"; B$(I), "Date FIRED:"; C$(I)
430 PRINT
440 PRINT
450 PRINT "No Record Found!. Would you like to try again?. (Y/N)"; F$
451  INPUT F$
452 IF SUBSTR$(UCASE$(F$), 1, 1) = "N" THEN GOTO 480
453 IF SUBSTR$(UCASE$(F$), 1, 1) = "Y" THEN GOTO 500
480  END
481  NEXT I
500 GOTO 350
1000 REM - DATA:
1010 REM - Number Of Records:
1020 DATA 4
1030 REM - N$=Date HIRED, B$=Soc. Sec. Number, C$=Date FIRED
1040 DATA "01-25-2001", "331-01-5981", "09-11-2011"
1050 DATA "03-30-1981", "053-00-9153", "01-20-2000"
1060 DATA "11-13-1983", "004-35-5391", "12-13-2009"
1070 DATA "10-29-2011", "365-76-1200", "10-30-2013"
9999 END
************************************************************
What changes should I make to BOTH lines: 452 and 453 ?. I'm using QuickBASIC versions: 1.1, 4.5 and, 7.1 but, without the HELP module installed.

Thank you,
Michael.

Answer
Hi Michael,

Sorry it took me a while to get back to you.

I got my languages mixed up, so this should fix it for you:

452 IF MID$(UCASE$(F$), 1, 1) = "N" THEN GOTO 480
453 IF MID$(UCASE$(F$), 1, 1) = "Y" THEN GOTO 500

Sorry about that.  The perils of knowing a few languages too many.

I hope that helps,
-Alex

Qbasic, Quickbasic

All Answers


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

©2016 About.com. All rights reserved.