•
•
•
•
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
![]() |
| |
•
•
Join Date: Nov 2007
Posts: 10
Reputation:
Rep Power: 2
Solved Threads: 0
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
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.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
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.
Then just loop through the array and find index of the oldest person. Once done, print that record.
Hope this helps.
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.
•
•
Join Date: Nov 2007
Posts: 10
Reputation:
Rep Power: 2
Solved Threads: 0
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 ?
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.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
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:
The first thing to read is a string, all by itself on a line, so
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:
Don't forget that there is still that blank line to read:
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:
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.
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)
var dateString: string; ... readln( fileA, dateString ); 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)
function readPersonRecordFromFile( var file: TextFile ): tPerson; var dateStr: string; begin readln( file, result.name ); readln( file, dateStr ); result.dateOfBirth = strToDate( dateStr ); readln( file ) 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.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
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:
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.
Delphi Syntax (Toggle Plain Text)
function readPersonRecordFromFile( var f: TextFile ): tPerson; var dateStr: string; begin readln( f, result.name ); readln( f, dateStr ); result.dateOfBirth := strToDate( dateStr ); readln( f ) end;
Sorry. Hope this helps.
Last edited by Duoas : Nov 3rd, 2007 at 7:28 am.
•
•
Join Date: Nov 2007
Posts: 10
Reputation:
Rep Power: 2
Solved Threads: 0
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
Name
tring;
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
heres what my code looks like:
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils;
type Tperson = record
Name
tring;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.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
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:
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.
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.
•
•
Join Date: Nov 2007
Posts: 10
Reputation:
Rep Power: 2
Solved Threads: 0
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
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
•
•
Join Date: Nov 2007
Posts: 10
Reputation:
Rep Power: 2
Solved Threads: 0
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:
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.•
•
Join Date: Nov 2007
Posts: 10
Reputation:
Rep Power: 2
Solved Threads: 0
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
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Pascal and Delphi Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Direction requested (Site Layout and Usability)
- Straight C (C++)
- Explorer running 99% CPU (Viruses, Spyware and other Nasties)
- URGENT HELP NEEDED - critically important...!!! (Windows NT / 2000 / XP / 2003)
- are all these needed (Windows NT / 2000 / XP / 2003)
- help much needed !! (OS X)
- New cdrw drive = boot up problems (Storage)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: help!!!
- Next Thread: i need help about my homework



Hybrid Mode