Computer Science Canada

Prity easy, but cant figure it out!

Author:  styxx [ Fri May 06, 2005 1:39 pm ]
Post subject:  Prity easy, but cant figure it out!

Alright I have been trying to make this work but it does'nt seem to want to... its a simply phone book with a file called direct.t (with a bunch of names & numbers) that it opens and reads. Then once you type in the name of the person it should output the name & number but it doesnt work.

code:
const maxEntries := 50
var name : array 1 .. maxEntries of string (20)

var phoneNumber : array 1 .. maxEntries of string (8)

var count : int := 0

var directory : int

open : directory, "direct.t", get



loop

    get : directory, skip
    exit when eof (directory)
    count := count + 1
    assert count <= maxEntries

    get : directory, name (count) : 20,
        phoneNumber (count) : 8

end loop

close : directory

put "There are ", count, " entries in the directory"

var friend : string
loop
    put "Enter friend's name:" ..
    exit when eof
    get friend : *
    if length (friend) < 20 then
        friend := friend + repeat (" ", 20 - length (friend))
    end if

    var place : int := 0

    for i : 1 .. count
        if name (i) = friend then
            place := i
            exit
        end if
    end for
    if place not= 0 then
        put "Phone number is ", phoneNumber (place)
    else
        put "Not in the list"
    end if
end loop


thanks in advance!

Author:  Token [ Fri May 06, 2005 4:46 pm ]
Post subject: 

its because the file with the names and numbers isnt there, so put this in front of your code but after your variables

code:

if not File.Exists ("direct.t") then %%%checks to see if the file is there
    open : directory, "direct.t", put%%opens/creates the file
    close : directory

end if

you have to check and not just create it incase you already had a list it would overwrite it.

Author:  styxx [ Fri May 06, 2005 5:21 pm ]
Post subject: 

Changed it and still does'nt work Confused ... any other ideas?

Author:  Token [ Fri May 06, 2005 6:58 pm ]
Post subject: 

well i didnt realise that was the problem, what you have to do it make a record, that would look like this

code:

type phonerecord :
    record
        name : string
        phoneNumber : string
    end record

then make that record into an array.
code:

var entry : array 1 .. maxEntries of phonerecord


okay so now you read from the file and assign the name and number accordingly
code:

    entry (i).name:=
    entry(i). phoneNumber:=



then what you do is get the name you want to search for and cyce through the array searching to see

code:

for i:1.. (number of entries)
    if entry (i).name = friend then
        put     entry (i).name :20,    entry(i). phoneNumber :7
    end if
end for


and there you have it!
hope it helps


i was thinking about making a tutorial for databases and searching through databases...

BTW. styx kicks ass (the band that is) Laughing


: