User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Pascal and Delphi section within the Software Development category of DaniWeb, a massive community of 456,587 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,607 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Pascal and Delphi advertiser: Programming Forums
Views: 1556 | Replies: 18
Reply
Join Date: Nov 2007
Posts: 10
Reputation: ickle2 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
ickle2 ickle2 is offline Offline
Newbie Poster

Help Needed Straight Away..

  #1  
Nov 2nd, 2007
My Task is....(this is in pascal code by the way i am using a software called pascal7)

Produce a program that:
*reads in, from a text file, the names and dates of birth of 5 people
*stores these names and dates of birth using a sensible data structure
*determines which person is the oldest
*writes to the screen the name and age (in years) of the oldest person.


My text file i wrote in windows notepad heres whats in it:

Rachel Aspinall
7/01/1993

Heather Aspinall
28/12/1990

Laura Prescott
17/02/1991

Simon Jenkins
20/03/1991

Ross Harrison
30/02/1990

And heres my code ive written in pascal so far:

program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils;

var
FileA:TextFile;
s: string;


begin

Assign(FileA,'PeopleInformation.txt');
Reset(FileA);
while not EOF(FileA) do
begin
Readln(FileA,s);
end;


end.

The code ive written so far is the first bullet point can someone help me with the other bullet points im desperate! i spent 5 hours on it yesterday and didnt get anywere i know i have to do an array i just dont know were to start please help
Last edited by ickle2 : Nov 2nd, 2007 at 6:24 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Help Needed Straight Away..

  #2  
Nov 2nd, 2007
Whenever you read the word "structure" always think record (or class, but you haven't gotten that far yet...).

So create a record with a string for the person's name and some numbers for his birth date.

Then, when reading the file, be careful to pay attention to the shape of the input. In this case, each input record has three lines:
name
birth date
blank line
So for each record you read you will have to read three lines.

You can use dynamic arrays, but if you haven't been taught that yet, just make an array of 100 or so records, and count how many records you put in the array as you read the file.
while not eof do
  begin
  read record
  number_of_records := number_of_records + 1;
  store the record you just read at the next empty location in the array
  end

Then just loop through the array and find index of the oldest person. Once done, print that record.

Hope this helps.
Reply With Quote  
Join Date: Nov 2007
Posts: 10
Reputation: ickle2 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
ickle2 ickle2 is offline Offline
Newbie Poster

Re: Help Needed Straight Away..

  #3  
Nov 2nd, 2007
Ive written this ... as a type

type Tperson = record
Name: string;
Dateofbirth: TDateTime;
end;{record}

i dont understand do i write anything else on my text document, seen on my last post?

how does it know what type i want?

what else do i add as a vairable would i put FileA:file of tperson ?
Last edited by ickle2 : Nov 2nd, 2007 at 8:16 pm.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Help Needed Straight Away..

  #4  
Nov 2nd, 2007
That is a very good start. Now all you need to do is read data from the file and stick it in the record.

For the following examples, I have the hypothetical variable:
var person: tPerson;

The first thing to read is a string, all by itself on a line, so
readln( fileA, person.name );
works just fine.

In the Delphi IDE, place the cursor over the word TDateTime and press F1. You'll get the help for that type. Up at the top there is a link that says See also. This will give you a list of things related to the tDateTime, including functions to read and write it from string. I found StrToDate in the list. So, you can read the next line as:
  1. var dateString: string;
  2. ...
  3. readln( fileA, dateString );
  4. person.dateOfBirth = strToDate( dateString );

Don't forget that there is still that blank line to read:
readln( fileA );

Put these three together and you have read a single record from file and stored it in a Pascal-style record.

You could make a function that does this for you:
  1. function readPersonRecordFromFile( var file: TextFile ): tPerson;
  2. var dateStr: string;
  3. begin
  4. readln( file, result.name );
  5. readln( file, dateStr );
  6. result.dateOfBirth = strToDate( dateStr );
  7. readln( file )
  8. end;

Keep working at it. Hope this helps.

PS. I'm not too fond of just giving code away when you are learning something new. Please pay attention to the thought process that went into creating this function. Now, you must still use the function to read all the "records" in the file into your array of tPerson.
Last edited by Duoas : Nov 2nd, 2007 at 10:56 pm.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Help Needed Straight Away..

  #5  
Nov 3rd, 2007
Er, it just occurred to me that file is a reserved word, not a directive, so it is illegal to use as a variable name. Also, I made a syntax error (using = instead of :=). So the code should be:
  1. function readPersonRecordFromFile( var f: TextFile ): tPerson;
  2. var dateStr: string;
  3. begin
  4. readln( f, result.name );
  5. readln( f, dateStr );
  6. result.dateOfBirth := strToDate( dateStr );
  7. readln( f )
  8. end;
I'd have edited my last post but it seems that after a certain amount of time passes you can't...

Sorry. Hope this helps.
Last edited by Duoas : Nov 3rd, 2007 at 7:28 am.
Reply With Quote  
Join Date: Nov 2007
Posts: 10
Reputation: ickle2 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
ickle2 ickle2 is offline Offline
Newbie Poster

Re: Help Needed Straight Away..

  #6  
Nov 3rd, 2007
An error occured when i tryed to play my code
heres what my code looks like:

program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils;




type Tperson = record
Nametring;
Dateofbirth:TDateTime;
end;{record}




var

s: string;
personinfo:Tperson;
FileA:textfile;
datestring:string;



begin

Assign(FileA,'PeopleInformation.txt');
Reset(FileA);
while not EOF(FileA) do
begin
Readln(FileA,s);
end;
close(FileA);
Reset (FileA);
while not EOF(FileA) do
begin
Readln (FileA,personinfo.name);
Readln (FileA,datestring);
personinfo.Dateofbirth:=strToDate(datestring);
Readln ( FileA );
end;















end.

It says 28/12/1990 is not a valid date but it is sint it?
I would also like to thank you for your help so far it is much appreicated
Last edited by ickle2 : Nov 3rd, 2007 at 8:52 am.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Help Needed Straight Away..

  #7  
Nov 3rd, 2007
Please use [code] blocks...

The default date format is 'm/d/yyyy', which is typical in the USA. I didn't look that closely at your dates... Before you call StrToDate say:
ShortDateFormat := 'd/mm/yyyy';
Now the date/time functions will work.

The last date in your input is 30/02/1990 --which is invalid. February has no more than 29 days... and 1990 is not a leap year so the latest day in February is 28/02/1990.

Sorry about that.
Reply With Quote  
Join Date: Nov 2007
Posts: 10
Reputation: ickle2 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
ickle2 ickle2 is offline Offline
Newbie Poster

Re: Help Needed Straight Away..

  #8  
Nov 3rd, 2007
ok ive sorted that error now because i just turned all my dates as 01/12/1990, 02/12/1990 ect..

but when i play the program it comes up as :

0.00000000000E+0000

heres my code:





var

s: string;
FileA:textfile;
datestring:string;
personarray:array [1..5] of Tperson;
personinfo:Tperson;
I:Integer;


begin

Assign(FileA,'PeopleInformation.txt');
Reset(FileA);
while not EOF(FileA) do
begin
Readln(FileA,s);
end;
close(FileA);
Reset (FileA);
while not EOF(FileA) do
begin
Readln (FileA,personinfo.name);
Readln (FileA,datestring);
personinfo.Dateofbirth:=strToDate(datestring);
Readln ( FileA );
end;
writeln (personarray[1].name, personarray[1].Dateofbirth);
readln;















end.

I dont understand why its doing this everthing seems to be fine
Reply With Quote  
Join Date: Nov 2007
Posts: 10
Reputation: ickle2 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
ickle2 ickle2 is offline Offline
Newbie Poster

Re: Help Needed Straight Away..

  #9  
Nov 3rd, 2007
ok now i have no date errors but again when i want to play the program it comes up as 00000000E+0000

heres my code:
program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;




 type Tperson = record
 Name:String;
 Dateofbirth:TDateTime;
 end;{record}




var

s: string;
FileA:textfile;
datestring:string;
personarray:array [1..5] of Tperson;
personinfo:Tperson;
I:Integer;


begin

   Assign(FileA,'PeopleInformation.txt');
   Reset(FileA);
   while not EOF(FileA) do
      begin
      Readln(FileA,s);
      end;
 close(FileA);
 Reset (FileA);
 while not EOF(FileA) do
 begin
 Readln (FileA,personinfo.name);
 Readln (FileA,datestring);
 ShortDateFormat := 'd/mm/yyyy';
 personinfo.Dateofbirth:=strToDate(datestring);
 Readln ( FileA );
 end;
 writeln (personarray[1].name, personarray[1].Dateofbirth);
 readln;















end.
Reply With Quote  
Join Date: Nov 2007
Posts: 10
Reputation: ickle2 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
ickle2 ickle2 is offline Offline
Newbie Poster

Re: Help Needed Straight Away..

  #10  
Nov 3rd, 2007
hey i edited my code again this time i know for certain its too do with the date when i do writeln (personinfo.name, personinfo.Dateofbirth);
readln; the date of birth displays like this:3.4000+E 0000

heres my code again

 Dateofbirth:TDateTime;
 end;{record}




var

s: string;
FileA:textfile;
datestring:string;
personinfo:Tperson;
I:Integer;


begin
   //Read whats on the text file:
   Assign(FileA,'PeopleInformation.txt');
   ShortDateFormat := 'dd/mm/yyyy';
   Reset(FileA);
   while not EOF(FileA) do
      begin
      Readln(FileA,s);
      end;
      //Store data from text file as types:
 close(FileA);
 Reset (FileA);
 while not EOF(FileA) do
 begin
 Readln (FileA,personinfo.name);
 Readln (FileA,datestring);
 personinfo.Dateofbirth:=strToDate( datestring );
 Readln ( FileA );
 end;
 writeln (personinfo.name, personinfo.Dateofbirth);
 readln;















end.
Last edited by ickle2 : Nov 3rd, 2007 at 5:45 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Pascal and Delphi Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Pascal and Delphi Forum

All times are GMT -4. The time now is 6:34 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC