944,093 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Nov 2nd, 2007
0

Help Needed Straight Away..

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ickle2 is offline Offline
10 posts
since Nov 2007
Nov 2nd, 2007
0

Re: Help Needed Straight Away..

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.
Pascal and Delphi Syntax (Toggle Plain Text)
  1. while not eof do
  2. begin
  3. read record
  4. number_of_records := number_of_records + 1;
  5. store the record you just read at the next empty location in the array
  6. end

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

Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 2nd, 2007
0

Re: Help Needed Straight Away..

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ickle2 is offline Offline
10 posts
since Nov 2007
Nov 2nd, 2007
0

Re: Help Needed Straight Away..

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:
Delphi Syntax (Toggle Plain Text)
  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:
Delphi Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 3rd, 2007
0

Re: Help Needed Straight Away..

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:
Delphi Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 3rd, 2007
0

Re: Help Needed Straight Away..

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ickle2 is offline Offline
10 posts
since Nov 2007
Nov 3rd, 2007
0

Re: Help Needed Straight Away..

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 3rd, 2007
0

Re: Help Needed Straight Away..

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ickle2 is offline Offline
10 posts
since Nov 2007
Nov 3rd, 2007
0

Re: Help Needed Straight Away..

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:
Pascal and Delphi Syntax (Toggle Plain Text)
  1. program Project1;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils;
  7.  
  8.  
  9.  
  10.  
  11. type Tperson = record
  12. Name:String;
  13. Dateofbirth:TDateTime;
  14. end;{record}
  15.  
  16.  
  17.  
  18.  
  19. var
  20.  
  21. s: string;
  22. FileA:textfile;
  23. datestring:string;
  24. personarray:array [1..5] of Tperson;
  25. personinfo:Tperson;
  26. I:Integer;
  27.  
  28.  
  29. begin
  30.  
  31. Assign(FileA,'PeopleInformation.txt');
  32. Reset(FileA);
  33. while not EOF(FileA) do
  34. begin
  35. Readln(FileA,s);
  36. end;
  37. close(FileA);
  38. Reset (FileA);
  39. while not EOF(FileA) do
  40. begin
  41. Readln (FileA,personinfo.name);
  42. Readln (FileA,datestring);
  43. ShortDateFormat := 'd/mm/yyyy';
  44. personinfo.Dateofbirth:=strToDate(datestring);
  45. Readln ( FileA );
  46. end;
  47. writeln (personarray[1].name, personarray[1].Dateofbirth);
  48. readln;
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. end.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ickle2 is offline Offline
10 posts
since Nov 2007
Nov 3rd, 2007
0

Re: Help Needed Straight Away..

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

Pascal and Delphi Syntax (Toggle Plain Text)
  1. Dateofbirth:TDateTime;
  2. end;{record}
  3.  
  4.  
  5.  
  6.  
  7. var
  8.  
  9. s: string;
  10. FileA:textfile;
  11. datestring:string;
  12. personinfo:Tperson;
  13. I:Integer;
  14.  
  15.  
  16. begin
  17. //Read whats on the text file:
  18. Assign(FileA,'PeopleInformation.txt');
  19. ShortDateFormat := 'dd/mm/yyyy';
  20. Reset(FileA);
  21. while not EOF(FileA) do
  22. begin
  23. Readln(FileA,s);
  24. end;
  25. //Store data from text file as types:
  26. close(FileA);
  27. Reset (FileA);
  28. while not EOF(FileA) do
  29. begin
  30. Readln (FileA,personinfo.name);
  31. Readln (FileA,datestring);
  32. personinfo.Dateofbirth:=strToDate( datestring );
  33. Readln ( FileA );
  34. end;
  35. writeln (personinfo.name, personinfo.Dateofbirth);
  36. readln;
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. end.
Last edited by ickle2; Nov 3rd, 2007 at 5:45 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ickle2 is offline Offline
10 posts
since Nov 2007

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: help!!!
Next Thread in Pascal and Delphi Forum Timeline: i need help about my homework





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


Follow us on Twitter


© 2011 DaniWeb® LLC