Help with searching for words (for a data base)

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Help with searching for words (for a data base)

Post by Sinuvoid » Sun Sep 23, 2007 6:10 pm

im trying to get this program to search for the name of teh person when typed and display their profile here the code.

Code: Select all

CLS
' menu
menu:
CLS
DO
COLOR 10, 0
LOCATE 5, 15
PRINT "DATABASE"
LOCATE 22, 30
PRINT "(2) ADD"
LOCATE 22, 5
PRINT "(1) SEARCH"

'my INKEY statment

kp$ = INKEY$
IF kp$ = "2" THEN GOTO addp
IF kp$ = "1" THEN GOTO searchp

'This is the end of the menu

LOOP

'This adds people to the database
addp:
CLS
COLOR 10, 0
INPUT "Age:", age$
INPUT "Name:", name$
INPUT "Personality:", per$
INPUT "Pasttime(s):", past$
INPUT "Comment:", com$

OPEN "database.txt" FOR APPEND AS #1
PRINT #1, "Age:"; age$, "Name:"; name$
PRINT #1, "Personality:"; per$
PRINT #1, "Pasttime(s):"; past$
PRINT #1, "Comment:"; com$
CLOSE #1

GOTO menu

'This searches for files
searchp:
CLS
OPEN "database.txt" FOR INPUT AS #1
INPUT #1, name$, age$, per$, past$, com$
INPUT "Search by persons name:", name$
IF name$ = name$ THEN
PRINT name$, age$, per$, past, com$
END IF
CLOSE #1

END

User avatar
sid6.7
Veteran
Posts: 318
Joined: Tue Jun 21, 2005 8:51 am
Location: west USA
Contact:

Post by sid6.7 » Sun Sep 23, 2007 6:48 pm

i made a simple database program called deebee here:

http://www.cruzan.info/comp/comp.html

if thats what you want...i'll go look up my old code..

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Sun Sep 23, 2007 7:28 pm

Code: Select all

"Revised, untested.
CLS 

DO 
COLOR 10, 0 
LOCATE 5, 15 
PRINT "DATABASE" 
LOCATE 22, 5 
PRINT "(1) SEARCH" 
LOCATE 22, 30 
PRINT "(2) ADD" 

DO
kp$ = INKEY$ 
LOOP UNTIL kp$ <> INKEY$

IF kp$ = "1" THEN SearchP
IF kp$ = "2" THEN AddP

LOOP    'This is the end of the menu 

SUB AddP
CLS
COLOR 10, 0
INPUT "Name:", name$ 
INPUT "Age:", age$ 
INPUT "Personality:", per$ 
INPUT "Pasttime(s):", past$ 
INPUT "Comment:", com$ 

OPEN "database.txt" FOR APPEND AS #1 
PRINT #1, name$, age$, per$, past$, com$ 
CLOSE #1 
END SUB

SUB SearchP
'This searches for files 
CLS 
OPEN "database.txt" FOR INPUT AS #1 
INPUT "Search by persons name:", name$ 
DO
 INPUT #1, name$, age$, per$, past$, com$ 
 IF name$ = name$ THEN PRINT name$, age$, per$, past, com$ 
LOOP UNTIL EOF(1)
CLOSE #1 
END SUB

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo » Sun Sep 23, 2007 9:00 pm

Lee,

The biggest problem that I notice quickly is that you prompt the user for the name and store it into the variable called "name$". Then, you read the database file and ALSO read the name field into the same variable called "name$".

Change the name of either of these two variable assignments. The user provided name into "name$" is OK, but change the input from the database to something like "namefile$".

You also need to change the IF to be:
if name$ = namefile$ then .,....

Regards..... Moneo

Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid » Mon Sep 24, 2007 6:10 am

Ok, none of your ideas are workin, PAtz i tried but it prints out EVERYTHING, moneo i tried but results in nothing and sid hasn't posted his code yet for his database that would be useful. remember im trying to get it to search for the name you typed in and show that names/persons profile.

User avatar
sid6.7
Veteran
Posts: 318
Joined: Tue Jun 21, 2005 8:51 am
Location: west USA
Contact:

Post by sid6.7 » Mon Sep 24, 2007 1:07 pm

here is the code that MONEO help me develop
this part if the search/edit function of the DB program...

Code: Select all

OPEN "c:\wtools\db.fil" FOR INPUT AS #3

'now we ask which last name you want to look at for editing purpose

LET foundsw = 0 'reset record found switch

INPUT "enter last name you want to look at"; answer$

LET answer$ = UCASE$(LTRIM$(RTRIM$(answer$)))

DO
  'get the record

  INPUT #3, rek.lame, rek.fame, rek.adres, rek.city, rek.state, rek.dept, rek.ssn, rek.hour, rek.phone
          
    IF RTRIM$(answer$) = RTRIM$(rek.lame) THEN
       LET foundsw = 1 'we found the record
       PRINT , RTRIM$(rek.lame), RTRIM$(rek.fame), RTRIM$(rek.adres)
       PRINT

        ELSE whatever....

i'll bet your problem has to do with trimming the KEY word your looking for....and what MONEO showed you on variable names above...

Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid » Mon Sep 24, 2007 3:02 pm

why do you use the .fil extension and not .txt? and what does .fil do?

ThemePark
Coder
Posts: 17
Joined: Wed Aug 22, 2007 6:37 pm

Post by ThemePark » Mon Sep 24, 2007 4:43 pm

Lee, the extension does not matter, you can use .fil, .txt, .doc, .dat or any other extension you want as long as it's 3 characters only. And .fil is just an abbreviation of File.

Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid » Mon Sep 24, 2007 5:12 pm

lmao, you dont understand my question =P what is the difference between .fil and .txt?

Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid » Mon Sep 24, 2007 6:04 pm

Ok nothing is working STILL...please,please help me
my code now

Code: Select all

CLS
' menu
menu:
CLS
DO
COLOR 10, 0
LOCATE 5, 25
PRINT "DATABASE OF HUMANS"
LOCATE 20, 55
PRINT "(2) ADD"
LOCATE 20, 5
PRINT "(1) SEARCH"

'my INKEY statment

kp$ = INKEY$
IF kp$ = "2" THEN GOTO addp
IF kp$ = "1" THEN GOTO searchp

'This is the end of the menu

LOOP

'This adds people to the database
addp:
CLS
COLOR 10, 0
INPUT "Age:", age%
PRINT
INPUT "First Name:", firstname$
PRINT
INPUT "Last Name:", lname$
PRINT
INPUT "Personality:", per$
PRINT
INPUT "Pasttime(s):", past$
PRINT
INPUT "Comment:", com$

OPEN "database.txt" FOR APPEND AS #1
PRINT #1, "Age:"; age%, "First Name:"; firstname$, "Last Name:"; 

lname$
PRINT #1, "Personality:"; per$
PRINT #1, "Pasttime(s):"; past$
PRINT #1, "Comment:"; com$
CLOSE #1

GOTO menu

'This searches for files
searchp:
CLOSE #3
CLS
foundp = 0
OPEN "database.txt" FOR INPUT AS #3

namefile$ = UCASE$(LTRIM$(RTRIM$(namefile$)))

INPUT #3, firstname$, lname$, age%, per$, past$, com$

INPUT "Search by persons name:", namefile$
IF RTRIM$(namefile$) = RTRIM$(firstname$) THEN
foundp = 1
PRINT firstname$, lname$, age%, per$, past, com$
GOTO tryagain
ELSE
GOTO notf
END IF
DO
notf:
LOCATE 2, 1
PRINT "Not Found"
tryagain:
LOCATE 3, 1
PRINT "Try Again?(y/n)"
kp$ = INKEY$
IF kp$ = "y" THEN GOTO searchp
IF kp$ = "n" THEN END
CLOSE #3
LOOP



User avatar
sid6.7
Veteran
Posts: 318
Joined: Tue Jun 21, 2005 8:51 am
Location: west USA
Contact:

Post by sid6.7 » Mon Sep 24, 2007 6:40 pm

the way you've done it if you dont find the record
on the first try you close the file.....then you re-open
and look at the first record again...an endless loop

you need to close the file ONLY when you've FOUND the record...

so if you dont find it you need to go read a new record..not close the file...



i think...

User avatar
sid6.7
Veteran
Posts: 318
Joined: Tue Jun 21, 2005 8:51 am
Location: west USA
Contact:

Post by sid6.7 » Mon Sep 24, 2007 6:41 pm

Lee wrote:lmao, you dont understand my question =P what is the difference between .fil and .txt?

he was right....there is no difference i just named it that way cause i want to....you can give any file you want any extension you want...

.boo
.too
.you
.xyz

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo » Mon Sep 24, 2007 9:00 pm

Lee,
You have serious problems with your input and output to/from the file.

PRINT #1, "Age:"; age%, "First Name:"; firstname$, "Last Name:"; Lname$
PRINT #1, "Personality:"; per$
PRINT #1, "Pasttime(s):"; past$
PRINT #1, "Comment:"; com$

PROBLEM #1: The way you have the above output, you will create 4 separate records. The last 3 records will be for Personality, Pastime and Comment. To keep them all on the samee record, you need a semicolon after Lname$, after per$, and after past$.

PROBLEM #2: Why are you writing the field headings like "Age", "First Name", etc. in front of every data field? Later when you input the record, you will still have the field headings on each record, which means that to search for a field, you have to be able to skip over the field heading with exactly the number of characters that it was written with.

PROBLEM #3: The Age field is written as the first field in the record, but later when you read from the file, you are positioning it as the third field.
INPUT #3, firstname$, lname$, age%, per$, past$, com$

PROBLEM #4: You use "INPUT #3" to read the records and you used comma delimiters when you wrote the record. This is is tricky stuff. I never use "INPUT #" because I've had problems with this. Like what happens if the user enters multiple pasttimes with spaces?

I only use "LINE INPUT" after having formatted each output record in a fixed format. But, I hope you can get it to work for you.

Regards..... Moneo

Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid » Tue Sep 25, 2007 6:05 am

The reason why I put "Age:"; age$ is because when i write it to the file that label will be beside it for fast identifacation, but if i dont need em jsut tell me :)

Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid » Tue Sep 25, 2007 6:15 am

ok fixed up code to what you guys told me, also tried moneo's LINE INPUT but still askes me for a #..what should i do?

Code: Select all

CLS
' menu
menu:
CLS
DO
COLOR 10, 0
LOCATE 5, 25
PRINT "DATABASE OF HUMANS"
LOCATE 20, 55
PRINT "(2) ADD"
LOCATE 20, 5
PRINT "(1) SEARCH"

'my INKEY statment

kp$ = INKEY$
IF kp$ = "2" THEN GOTO addp
IF kp$ = "1" THEN GOTO searchp

'This is the end of the menu

LOOP

'This adds people to the database
addp:
CLS
COLOR 10, 0
INPUT "Age:", age%
PRINT
INPUT "First Name:", firstname$
PRINT
INPUT "Last Name:", lname$
PRINT
INPUT "Personality:", per$
PRINT
INPUT "Pasttime(s):", past$
PRINT
INPUT "Comment:", com$

OPEN "database.txt" FOR APPEND AS #1
PRINT #1,age%, firstname$, lname$,per$, past$,com$
CLOSE #1

GOTO menu

'This searches for files
searchp:
CLOSE #3
CLS
foundp = 0
OPEN "database.txt" FOR INPUT AS #3

namefile$ = UCASE$(LTRIM$(RTRIM$(namefile$)))

INPUT #3, age%,firstname$, lname$, per$, past$, com$

INPUT "Search by persons name:", namefile$
searchf:
IF RTRIM$(namefile$) = RTRIM$(firstname$) THEN
foundp = 1
PRINT age%,firstname$, lname$, per$, past, com$
GOTO tryagain
ELSE
GOTO searchf
END IF
DO
notf:
LOCATE 2, 1
PRINT "Not Found"
tryagain:
DO
LOCATE 3, 1
PRINT "Try Again?(y/n)"
kp$ = INKEY$
IF kp$ = "y" THEN GOTO searchp
IF kp$ = "n" THEN END
CLOSE #3
LOOP
LOOP


Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Post by Mac » Tue Sep 25, 2007 2:44 pm

Lee wrote:..what should i do?
What you should do is work on something easier. Your code had so many problems I couldn't explain them one at a time.

If you insist on working on something that is over your head, you can use the code I put below for a starter.

Mac

Code: Select all

CLS
' menu
menu:
CLS
DO
COLOR 10, 0
LOCATE 5, 25
PRINT "DATABASE OF HUMANS"
LOCATE 20, 55
PRINT "(2) ADD"
LOCATE 20, 5
PRINT "(1) SEARCH"

'my INKEY statment

kp$ = INKEY$
IF kp$ = "2" THEN GOTO addp
IF kp$ = "1" THEN GOTO searchp

'This is the end of the menu

LOOP

'This adds people to the database
addp:
CLS
COLOR 10, 0
INPUT "Age:", age%
PRINT
LINE INPUT "First Name:", firstname$
PRINT
LINE INPUT "Last Name:", lname$
PRINT
LINE INPUT "Personality:", per$
PRINT
LINE INPUT "Pasttime(s):", past$
PRINT
LINE INPUT "Comment:", com$

OPEN "database.txt" FOR APPEND AS #1
WRITE #1, age%, firstname$, lname$, per$, past$, com$
CLOSE #1

GOTO menu

'This searches for people
searchp:
DO
  CLS
  LINE INPUT "Search by persons first name:", ffname$
  ffname$ = UCASE$(LTRIM$(RTRIM$(ffname$)))
  OPEN "database.txt" FOR INPUT AS #3
  Found = 0
  DO WHILE NOT EOF(3)
    INPUT #3, age%, firstname$, lname$, per$, past$, com$
    IF ffname$ = UCASE$(LTRIM$(RTRIM$(firstname$))) THEN
      PRINT age%, firstname$, lname$, per$, past$, com$
      Found = 1
    END IF
  LOOP
  CLOSE #3
  IF Found = 0 THEN
    LOCATE 2, 1
    PRINT "Not Found"
  END IF
  DO
    LOCATE , , 1: PRINT "Try Again?(y/n): ";
    kp$ = INPUT$(1)
    PRINT kp$
    kp$ = UCASE$(kp$)
    IF kp$ = "N" THEN END
  LOOP WHILE kp$ <> "Y"
LOOP

Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid » Tue Sep 25, 2007 3:01 pm

Ok, everything is still not working and I directly made Mac's cod3e into its own bas and it still didnt work, can someone jsut try and make it to search for the persons first name and show their profile? I will put that person who makes teh code part of teh owner. thanks! :)

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Tue Sep 25, 2007 8:00 pm

OK, seeing moneo's comment, I saw that stupid mistake. *head meets desk* Try this...

Code: Select all

'Revised, untested.
CLS 

DO 
COLOR 10, 0 
LOCATE 5, 15 
PRINT "DATABASE" 
LOCATE 22, 5 
PRINT "(1) SEARCH" 
LOCATE 22, 30 
PRINT "(2) ADD" 

DO
kp$ = INKEY$ 
LOOP UNTIL kp$ <> INKEY$

IF kp$ = "1" THEN SearchP
IF kp$ = "2" THEN AddP

LOOP    'This is the end of the menu 

SUB AddP
CLS
COLOR 10, 0
INPUT "Name:", name$ 
INPUT "Age:", age$ 
INPUT "Personality:", per$ 
INPUT "Pasttime(s):", past$ 
INPUT "Comment:", com$ 

OPEN "database.txt" FOR APPEND AS #1 
PRINT #1, name$, age$, per$, past$, com$ 
CLOSE #1 
END SUB

SUB SearchP
'This searches for files 
CLS 
OPEN "database.txt" FOR INPUT AS #1 
INPUT "Search by persons name:", SearchName$ 
DO
 INPUT #1, name$, age$, per$, past$, com$ 
 IF SearchName$ = name$ THEN PRINT name$, age$, per$, past, com$ 
LOOP UNTIL EOF(1)
CLOSE #1 
END SUB

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo » Tue Sep 25, 2007 9:13 pm

Lee wrote:ok fixed up code to what you guys told me, also tried moneo's LINE INPUT but still askes me for a #..what should i do?
I'm sorry that I mentioned the LINE INPUT before. Like I had said, that will only work if you set up the fields in the record in a fixed format. You don't have the record set up this way, so forget it.

I tested Mac's version and it works perfectly, as expected. Mac never posts code that doesn't work. I don't understand why it didn't work for you. Download it again. I suggest you work on Mac's version and abandon your original version.

Regards..... Moneo

Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid » Thu Oct 25, 2007 6:17 am

Now its saying im past the end of the file heh...

Post Reply