954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to improve my programm in Pascal?

This is my programm which is to search in the data file.
I cannot search the data with key words/character but only the full words.
How can I correct my programm to become searching in key words/character?

The DATA:
20081231 Betty 03/09/1978

The programm
begin
write('Member ID > ');
readln(tid);
searching:=false;
assign(infile,infilename);
reset(infile);
while not eof(infile) do begin
readln(infile,id,na,bdy,bdm,bdd);
if id=tid then begin
writeln('Name:',na);
writeln('Date of birth: ',bdy,'-',bdm,'-',bdd);
searching:=true;
end;
end;
if searching=false then
writeln(tid,' cannot be found.');
close(infile)
end;

yozuca
Newbie Poster
2 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

See the helpfile for the function Pos(). It will allow you to search for a substring.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

you can use records to store the members data
like my code

{
The DATA:
20081231 Betty 03/09/1978

The programm
}
program solution;

type Tmember = record
                  id:longint;
                  name:string;
                  day:1..31;
                  month:1..12;
                  year:1900..3000;
               end;


var infile:file of Tmember;
    infilename:string;
    someone:Tmember;
    searching:boolean;
    idn:longint;


begin
     infilename:='C:\myfile.dat';
     write('Member ID > ');
     readln(idn);
     searching:=false;
     assign(infile,infilename);
     reset(infile);
     while not eof(infile) do begin
           read(infile,someone);
           if idn=someone.id then begin
           writeln('Name:',someone.name);
           writeln('Date of birth: ',someone.day,'-',someone.month,'-',someone.year);
           searching:=true;
           end;
     end;
     if searching=false then
     writeln(someone.id,' cannot be found.');
     close(infile)
end.


of course you can search any elements of the record in the file.

FlamingClaw
Posting Pro
559 posts since Feb 2009
Reputation Points: 132
Solved Threads: 138
 

I think I forget about writting a record to a file.....fixed.....

{full commented version created by FlamingClaw 2009}
program solution;

type Tmember = record
                  id:longint;
                  name:string;
                  day:1..31;
                  month:1..12;
                  year:1900..3000;
               end;


var infile:file of Tmember; {this file can contain records only...}
    infilename:string;      {stores the file's name ,path,and extension}
    someone,base:Tmember;   {two records,someone for read in,base for write in}
    searching:boolean;      {checking search,done it or not...}
    idn:longint;            {we search by id,and its type is longint same as in the record's field}

{here begins the main program}
begin
     {the file named myfile.dat and placed to the C:\ drive}
     infilename:='C:\myfile.dat';
     write('Member ID > ');
     {here catch the answer of the user}
     readln(idn);
     {searching is set to false}
     searching:=false;
     {assign the file var to a file,that is set to infilename }
     assign(infile,infilename);
     {create the data file,or if it is exists then delete its contents!}
     rewrite(infile);
     {fill the record's fields with these data:
     20081231 Betty 03/09/1978 }
     with (base) do begin
                      id:=20081231;
                      name:='Betty';
                      day:=03;
                      month:=09;
                      year:=1978;
     end;
     {first write the data file contents}
     write(infile,base);
     {file opening for reading...}
     reset(infile);
     {loop will over and over till we reach the end of the file}
     while not eof(infile) do begin
           {read one record,by round}
           read(infile,someone);
           {if the catched answer is equal to the readed record' id field then}
           if idn=someone.id then begin
           {write out the results to the screen,used by the record}
           writeln('Name:',someone.name);
           writeln('Date of birth: ',someone.day,'-',someone.month,'-',someone.year);
           {and the searching was success,everybody happy !}
           searching:=true;
           end;
     end;
     {if there were not success the loop then,search is stay false}
     if searching=false then
     {and notify the user...}
     writeln(someone.id,' cannot be found.');
     {it is not matter that the process was success or not close the file}
     close(infile);
     readln;{waits for pressing the enter button}
end.  {and here the end of the program}
FlamingClaw
Posting Pro
559 posts since Feb 2009
Reputation Points: 132
Solved Threads: 138
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: