943,724 Members | Top Members by Rank

Ad:
Sep 16th, 2009
-1

How to improve my programm in Pascal?

Expand Post »
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;
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yozuca is offline Offline
2 posts
since Sep 2009
Sep 16th, 2009
0

Re: How to improve my programm in Pascal?

See the helpfile for the function Pos(). It will allow you to search for a substring.
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 728
Bite my shiny metal ass!
pritaeas is offline Offline
4,166 posts
since Jul 2006
Sep 17th, 2009
1

Re: How to improve my programm in Pascal?

you can use records to store the members data
like my code
pascal Syntax (Toggle Plain Text)
  1.  
  2. {
  3. The DATA:
  4. 20081231 Betty 03/09/1978
  5.  
  6. The programm
  7. }
  8. program solution;
  9.  
  10. type Tmember = record
  11. id:longint;
  12. name:string;
  13. day:1..31;
  14. month:1..12;
  15. year:1900..3000;
  16. end;
  17.  
  18.  
  19. var infile:file of Tmember;
  20. infilename:string;
  21. someone:Tmember;
  22. searching:boolean;
  23. idn:longint;
  24.  
  25.  
  26. begin
  27. infilename:='C:\myfile.dat';
  28. write('Member ID > ');
  29. readln(idn);
  30. searching:=false;
  31. assign(infile,infilename);
  32. reset(infile);
  33. while not eof(infile) do begin
  34. read(infile,someone);
  35. if idn=someone.id then begin
  36. writeln('Name:',someone.name);
  37. writeln('Date of birth: ',someone.day,'-',someone.month,'-',someone.year);
  38. searching:=true;
  39. end;
  40. end;
  41. if searching=false then
  42. writeln(someone.id,' cannot be found.');
  43. close(infile)
  44. end.

of course you can search any elements of the record in the file.
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009
Sep 20th, 2009
0

Re: How to improve my programm in Pascal?

I think I forget about writting a record to a file.....fixed.....
pascal Syntax (Toggle Plain Text)
  1.  
  2. {full commented version created by FlamingClaw 2009}
  3. program solution;
  4.  
  5. type Tmember = record
  6. id:longint;
  7. name:string;
  8. day:1..31;
  9. month:1..12;
  10. year:1900..3000;
  11. end;
  12.  
  13.  
  14. var infile:file of Tmember; {this file can contain records only...}
  15. infilename:string; {stores the file's name ,path,and extension}
  16. someone,base:Tmember; {two records,someone for read in,base for write in}
  17. searching:boolean; {checking search,done it or not...}
  18. idn:longint; {we search by id,and its type is longint same as in the record's field}
  19.  
  20. {here begins the main program}
  21. begin
  22. {the file named myfile.dat and placed to the C:\ drive}
  23. infilename:='C:\myfile.dat';
  24. write('Member ID > ');
  25. {here catch the answer of the user}
  26. readln(idn);
  27. {searching is set to false}
  28. searching:=false;
  29. {assign the file var to a file,that is set to infilename }
  30. assign(infile,infilename);
  31. {create the data file,or if it is exists then delete its contents!}
  32. rewrite(infile);
  33. {fill the record's fields with these data:
  34.   20081231 Betty 03/09/1978 }
  35. with (base) do begin
  36. id:=20081231;
  37. name:='Betty';
  38. day:=03;
  39. month:=09;
  40. year:=1978;
  41. end;
  42. {first write the data file contents}
  43. write(infile,base);
  44. {file opening for reading...}
  45. reset(infile);
  46. {loop will over and over till we reach the end of the file}
  47. while not eof(infile) do begin
  48. {read one record,by round}
  49. read(infile,someone);
  50. {if the catched answer is equal to the readed record' id field then}
  51. if idn=someone.id then begin
  52. {write out the results to the screen,used by the record}
  53. writeln('Name:',someone.name);
  54. writeln('Date of birth: ',someone.day,'-',someone.month,'-',someone.year);
  55. {and the searching was success,everybody happy !}
  56. searching:=true;
  57. end;
  58. end;
  59. {if there were not success the loop then,search is stay false}
  60. if searching=false then
  61. {and notify the user...}
  62. writeln(someone.id,' cannot be found.');
  63. {it is not matter that the process was success or not close the file}
  64. close(infile);
  65. readln;{waits for pressing the enter button}
  66. end. {and here the end of the program}
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: question on TFileStream (Inno Setup)
Next Thread in Pascal and Delphi Forum Timeline: Delphi programs not responding when run outside of IDE





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC