address book searching

Hey all,

For a school project I have to create a program where the user writes a certain name, and the program has to search three text files in order to write out the last name and telephone number of that person. I've heard that this is simple but it's killing me... all I know is that there have to be arrays and loops, but I don't know how to use them. There are only 10 names in the database, so I suppose it shouldn't be too difficult...

ANY help would be appreciated! Thank you very very much!

Constance

Comments

  • More info needed. What is contained within the three files?
    Other than that, you can use this procedure to search through a text file to see if a name exists. If so it will return the line number that matched the search:

    [code]
    FUNCTION CheckFile(FileName, Name : String) : Integer;
    VAR
    TxtFile : Text;
    Line : String;
    LineNum : Integer;
    Begin
    CheckFile := -1;
    Assign(TxtFile, FileName); Reset(TxtFile);
    LineNum := 0;
    While Not(Eof(TxtFile)) Do
    Begin
    Inc(LineNum);
    ReadLn(TxtFile, Line);
    If (Line = Name) Then
    Begin
    WriteLn('FOUND ' + Name + ' AT LINE #', LineNum);
    CheckFile := LineNum;
    End;
    End;
    Close(TxtFile);
    End;
    [/code]
  • : For a school project I have to create a program where the user
    : writes a certain name, and the program has to search three text
    : files in order to write out the last name and telephone number of
    : that person. I've heard that this is simple but it's killing me...
    : all I know is that there have to be arrays and loops, but I don't
    : know how to use them. There are only 10 names in the database, so I
    : suppose it shouldn't be too difficult...
    :

    Try to be a bit more explicit, like, what's the format of your database, post some examples etc. Knowing nothing specific I can only help with a pseudo code using one textfile, assuming that the records are stored in one line each:[code]ask user for "name"
    open text file for read
    loop { [green]<---- usually done with a [b]while[/b] loop[/green]
    read a line from file
    examine line (eg compare a part of it with "name")
    if match found: output line, then break loop
    } until are no more lines to read (reached end of file)
    close text file[/code]
  • They're text files. One contains names, the other contains last names, and the third contains text files. There has to be a 'main screen' where we choose by which criteria we want to search, a loop to bring it back to the beginning, and of course the search function itself.

    At some point I guess I have to read a certain portion of the array used, {readln (ulaz1, A[i]);} but then an error reads that pascal is not able to read or write variables like this....

    Sorry I wasn't explicit the first time around, I'm pretty confused myself! Thank you, though!
  • The code I posted above should work. It will let you search through whichever file you specify and return the line #. You can then have a second function to retrieve a given line # from the file.

    You text file I imagine look like this:

    [code]
    Justin
    Amy
    John
    Daphny
    [/code]

    [code]
    Owens
    Valentine
    Doe
    Richards
    [/code]

    [code]
    (415) 555-0001
    (777) 555-0002
    (230) 555-0003
    (990) 555-0004
    [/code]

    You could the above function to search for "John". It would return line #3. Then you can create a second function to read out line #3 from the other two files, "Doe" & "(230) 555-0003"

    If you need more help, post the code you have (and remember to use the code tags <> )
  • Thank you so much!!! You saved my life!! :):):)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories