Qbasicnews.com

Full Version: Phone book
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
I want to make a phone book that loads phone numbers from a file and also has the feature for adding numbers to the file.

i got to the point were it would load the phone numbers but i dont really get how you would do that. i want to be able to get the numbers and assign them to a DATA, name then the number. how would i do this :-?
No need for DATA

You can use random access (I am not completly familar with this) or INPUT/OUPUT to files

INPUT/OUTPUT stores it in plain text and you can have multiple subs to do what you want (and arrays so you aren't always reading and writing to the file)

with the Load sub you have it INPUT all the names and numbers into the array
with the Save sub you have it OUTPIT all the names and numbers into the file (overwriting whatever is already in there)

to add a number to the file you'd have to just expand the array (or you can start with a big one and just add one into the empty one)
to delete you'd have to just make the array space empty

(gah I am bad at explaining)
?????? :-? :-? :-?
Smile If you use INPUT/OUTPUT you need to make a parser to search for and separate all the numbers into their own arrays.. or Load in order, retreave in order to how you want the file read..

Then you got random files which I've never got around to messing with, course Meg seems to know more on those if you want that.. :wink:

I like parsers, but thats just me,...
could you please post an example?
using INPUT #1... it has a delimeter of `,`
LINE INPUT #1 gets the whole line.

Code:
CLS
OPEN "test1.txt" FOR INPUT AS #1
'#Open file

TYPE type1
a AS STRING * 5
b AS STRING * 5
END TYPE
DIM moo(1 TO 5) AS type1
'#define moo as a single array with the UDT of `a` (string with e length of 5) and `b` (the same)
DO
i = i + 1
'#increment counter

INPUT #1, moo(i).a, moo(i).b
'#Inputs into variables (using `,` as the delimeter)

LOOP UNTIL EOF(1)
'#Loop until end of file

PRINT moo(3).a, moo(3).b
'#print
Quote:Ilike, 123
pie, 300
andso, 19786
shoul, 1233
j00, 10000
ok well i got this far can you tell me whats wrong with it?

Code:
DIM errorcode AS INTEGER
errorcode = 0
TYPE phonetype
  FullName AS STRING * 20
  Number AS STRING * 10
END TYPE
DIM phone(1 TO 100) AS phonetype

ON ERROR GOTO errorhandler
CLS
OPEN "C:\PhoneNumbers.txt" FOR INPUT AS #1
IF errorcode = 53 THEN GOTO createfile
DO
i = i + 1
INPUT #1, phone(i).FullName, phone(i).Number
LOOP UNTIL EOF(1)
CLOSE #1

FOR a = 1 TO 2
PRINT phone(a).FullName, phone(a).Number
NEXT


createfile:
INPUT "Type name of person you want to add", aName$
INPUT "What i their phone number", aNumber$
OPEN "C:\PhoneNumbers.txt" FOR APPEND AS #1
WRITE #1, aName$, aNumber$
CLOSE #1
INPUT "Do you want to add more? ", yn$
IF yn$ = "y" THEN GOTO createfile
END



errorhandler:
errorcode = ERR
RESUME NEXT

Anonymous

well, if its for qb, you cant use that filename, thats not a valid 8.3 filename (PhoneNumbers.txt)

also i would do "do while not eof(1)"

look, the whole point of using a type is that you dont have to "input#1, type(x).name, type(x).number", you should just be able to input type(x) and everything will be included.

when checking for y/n, do like this

IF UCASE$(yn$) = "Y" THEN GOTO createfile



anyways, yeah, hope my hints point u in the right direction
Quote:could you please post an example?
The below was coded from memory straight into these forums, I haven't tested it, and it hasn't been written to be run if loaded in QB you'll need to modify it so it doesn't just run through each of the stages Tongue
[syntax="QBASIC"]TYPE contact
FullName AS STRING * 20
Number AS STRING * 10
END TYPE

DIM person AS contact

filename$="fonebook.txt" 'Complies with DOS's 8.3 file names

'*** test to see if file exist's ***
OPEN filename$ FOR BINARY AS #1
IF LOF(1) = 0 THEN
CLOSE #1
KILL filename$
' Insert code for starting a new phone book (you could just go on to the write to file section
ELSE
CLOSE #1
' Insert code to continue adding to file.
END IF
'*** To write to the file ***
OPEN filename$ FOR RANDOM AS #1 LEN=LEN(person)

DO
numContacts = LOF(1) / LEN(person)
INPUT "Name :", person.fullname
INPUT "Number :", person.number
PUT #1, numContacts+1, person
PRINT "Press Escape to quit, any other key to continue"
DO
key$=INKEY$
LOOP UNTIL key$ <> ""
LOOP UNTIL key$ = CHR$(27)
CLOSE #1

'*** To read from the file ***
OPEN filename$ FOR RANDOM AS #1 LEN=LEN(person)
numContacts = LOF(1) / LEN(person)

FOR i = 1 TO numContacts
GET #1, , person
PRINT "Name :", person.fullname
PRINT "Number :", person.number
NEXT i
CLOSE #1

'*** To find a contact ***
OPEN filename$ FOR RANDOM AS #1 LEN=LEN(person)

'So the search string and record string are the same length
DIM searchName AS STRING * 20

INPUT "Please enter the name to search for", searchName

numContacts = LOF(1) / LEN(person)

FOR i = 1 TO numContacts
GET #1, , person
IF person.fullname = searchName THEN
PRINT "Name :", person.fullname
PRINT "Number :", person.number
EXIT FOR 'No need to search the rest of the file (you could ommit this line so the search brings up all instances not just the first)
END IF
NEXT i
CLOSE #1[/syntax]
If you use sequential access you need to use INPUT to read from the file into QB. OUTPUT to create a new file. APPEND to add data onto the end of a file. If you use OUTPUT on a file that already exists you'll overwrite the data already stored in the file. Using sequential access you would also need to recreate the whole file if you wanted to change some information inside it.

Random access is better for file's whos information changes. Sequential is only really usefull for logs and that type of write once data.

Using a combination of find contact and write to file you can edit a contact's details.

Also I don't like you idea of using QB's ON ERROR, hence I've provided you with a better way of checking if a file exists. This way if there is a problem with your code QB will flash up a message that means somehting and point you (hopfully) to the line that caused the problem.
ok wow im slightly confused remeber im just a newbie :???:
Pages: 1 2 3