•
•
•
•
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 423,401 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 4,550 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: 1422 | Replies: 18
![]() |
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation:
Rep Power: 11
Solved Threads: 193
Hey there. Here is your code more carefully formatted and with some commentary.
Note 1
Perhaps you made this loop because you wanted to see the file before putting it in a TPerson record? If so, replace the
coment with:
Otherwise, you keep giving s a new value each time through the loop but you never actually use it. Neither s nor the loop have any purpose --you could just delete it and the code would work the same.
Note 2
As already noted, you are making the same error here with personinfo as you did with s above. Each time through the loop you initialize it properly, but you never use it before initializing it again with a new person's data.
I think what you wanted to do is replace
with
This means though that you will have to use i as well. Before starting the loop set it to 1 and increment it by one at the end of each loop.
Note 3
You should always give a variable a value before you use it. Since you did not store your records in personarray (see note 2), but only in personinfo, the only variable that has information read from file is personinfo, and it is only the very last record in the file (Ross Harrison).
The problem you are having with the funny "00000000E+0000" output is this: since you never gave the personarray[1] record a value, the Name string is the empty string ('', i.e. a string with no characters in it), and the DateOfBirth is zero. So, when you ask to
it does just that, first printing
then printing
TDateTime is stored as a floating point double, so that is why the weird output. Remember you must use the StrToDate and DateToStr functions to convert a string to or from a tDateTime.
Hope this helps.
Delphi Syntax (Toggle Plain Text)
program Project1; {$APPTYPE CONSOLE} uses SysUtils; type Tperson = record Name: String; Dateofbirth: TDateTime; end; var s: string; FileA: textfile; datestring: string; personarray: array [1..5] of Tperson; // very good personinfo: Tperson; I: Integer; begin // You only need to set this global value once ShortDateFormat := 'd/mm/yyyy'; // Good Assign(FileA,'PeopleInformation.txt'); // The following reads each line of the file but // does nothing with it... Reset(FileA); while not EOF(FileA) do begin Readln(FileA,s); // See note 1 end; close(FileA); // The following is a loop that properly reads each // record from file. However, like the first loop's // s:string variable, the information is never used; // it is forgotten the next time through the loop. // See note 2 Reset (FileA); while not EOF(FileA) do begin Readln (FileA,personinfo.name); Readln (FileA,datestring); personinfo.Dateofbirth:=strToDate(datestring); Readln ( FileA ); end; // Here you try to print a variable you never initialized. // See note 3 writeln (personarray[1].name, personarray[1].Dateofbirth); // Wait for the user to press ENTER before terminating readln; end.
Note 1
Perhaps you made this loop because you wanted to see the file before putting it in a TPerson record? If so, replace the
// See note 1coment with:
writeln( s );Otherwise, you keep giving s a new value each time through the loop but you never actually use it. Neither s nor the loop have any purpose --you could just delete it and the code would work the same.
Note 2
As already noted, you are making the same error here with personinfo as you did with s above. Each time through the loop you initialize it properly, but you never use it before initializing it again with a new person's data.
I think what you wanted to do is replace
personinfo.somethingwith
personarray[ i ].somethingThis means though that you will have to use i as well. Before starting the loop set it to 1 and increment it by one at the end of each loop.
Note 3
You should always give a variable a value before you use it. Since you did not store your records in personarray (see note 2), but only in personinfo, the only variable that has information read from file is personinfo, and it is only the very last record in the file (Ross Harrison).
The problem you are having with the funny "00000000E+0000" output is this: since you never gave the personarray[1] record a value, the Name string is the empty string ('', i.e. a string with no characters in it), and the DateOfBirth is zero. So, when you ask to
writeln( myperson.Name, myperson.DateOfBirth );it does just that, first printing
then printing
00000000E+0000TDateTime is stored as a floating point double, so that is why the weird output. Remember you must use the StrToDate and DateToStr functions to convert a string to or from a tDateTime.
Hope this helps.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation:
Rep Power: 11
Solved Threads: 193
It doesn't matter. Just use the supplied functions to convert it to/from a string.
(I'm not being rude here. You should not care how it is actually stored in memory --only how to use it. I shouldn't have mentioned its storage type.)
If you want to learn more about floating-point data types see the section "Data types, variables, and constants" chapter in the Object Pascal Reference that came with Delphi.
(I'm not being rude here. You should not care how it is actually stored in memory --only how to use it. I shouldn't have mentioned its storage type.)
If you want to learn more about floating-point data types see the section "Data types, variables, and constants" chapter in the Object Pascal Reference that came with Delphi.
Last edited by Duoas : Nov 3rd, 2007 at 5:46 pm.
•
•
Join Date: Nov 2007
Posts: 10
Reputation:
Rep Power: 1
Solved Threads: 0
ive looked through your notes carefully and corrected my mistakes but im still having the same problem when reading the date it shows:
rachel aspinall 3.400E 00000
my edited code:
rachel aspinall 3.400E 00000
my edited code:
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils,DateUtils;
type Tperson = record
Name:String;
Dateofbirth: TDateTime;
end;{record}
var
FileA:textfile;
datestring:string;
personarray:array [1..5] of Tperson;
I:Integer;
begin
ShortDateFormat := 'd/mm/yyyy';
//Read whats on the text file:
Assign(FileA,'PeopleInformation.txt');
Reset(FileA);
I:=1;
while not EOF(FileA) do
begin
Readln (FileA,personarray[i].name);
Readln (FileA,datestring);
personarray[i].Dateofbirth:=strTodate(datestring);
Readln ( FileA );
I:=+1
end;
writeln (personarray[1].name, personarray[1].Dateofbirth);
readln;
end.•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation:
Rep Power: 11
Solved Threads: 193
The writeln procedure only knows how to format and write certain types of things: all of the simple data types (integer, byte, word, char, single, double, boolean, etc.) and strings. It is blissfully ignorant about everything else.
Hence, you can't say
Remember when you read the date you had to say
That's because TDateTime is an abstract data type, and you need functions to convert it to and from a string.
Hence, you can't say
writeln( personinfo ); --it just doesn't know how. The same is true of readln. You had to read into strings and convert things as needed.Remember when you read the date you had to say
readln( dateStr );personarray[ i ].dateOfBirth := strToDate( dateStr );That's because TDateTime is an abstract data type, and you need functions to convert it to and from a string.
Delphi Syntax (Toggle Plain Text)
uses SysUtils; var d: tDateTime; s: string; begin write( 'Please enter a date as M/D/Y: ' ); readln( s ); try d := strToDate( s ) except writeln( 'You did not enter a valid date.' ); halt end; ShortDateFormat := 'mmmm d, yyyy'; writeln( 'The date you entered was ', dateToStr( d ) ) end.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation:
Rep Power: 11
Solved Threads: 193
•
•
Join Date: Nov 2007
Posts: 10
Reputation:
Rep Power: 1
Solved Threads: 0
o i have another error for some reason evertime i try to play the program it goes : invalid date i dont understand because my text file is fine
text file:
Heather Aspinall
28/12/1990
Simon Jenkins
12/10/1991
Ross Harrison
14/10/1992
Rachel Aspinall
07/01/1993
Laura Prescott
12/09/1994
my code:
text file:
Heather Aspinall
28/12/1990
Simon Jenkins
12/10/1991
Ross Harrison
14/10/1992
Rachel Aspinall
07/01/1993
Laura Prescott
12/09/1994
my code:
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils,DateUtils;
type Tperson = record
Name:String;
Dateofbirth: TDateTime;
end;{record}
var
FileA:textfile;
datestring:string;
personarray:array [1..5] of Tperson;
Dateofbirth1,Dateofbirth2,Dateofbirth3,Dateofbirth4,Dateofbirth5:string;
I:Integer;
begin
//Read whats on the text file:
ShortDateFormat := 'dd/mm/yyyy';
Assign(FileA,'PeopleInformation.txt');
Reset(FileA);
I:=1;
while not EOF(FileA) do
begin
Readln (FileA,personarray[i].name);
Readln (FileA,datestring);
personarray[i].Dateofbirth:=StrToDate(datestring);
Readln ( FileA );
I:=I+1;
end;
Dateofbirth1:= dateToStr( personarray[1].Dateofbirth ) ;
Dateofbirth2:= dateToStr( personarray[2].Dateofbirth ) ;
Dateofbirth3:= dateToStr( personarray[3].Dateofbirth ) ;
Dateofbirth4:= dateToStr( personarray[4].Dateofbirth ) ;
Dateofbirth5:= dateToStr( personarray[5].Dateofbirth ) ;
writeln ('The oldest person is: ');
writeln (personarray[1].name );
readln;
end.•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation:
Rep Power: 11
Solved Threads: 193
I don't know. It works fine on my end.
Question: why all the DateOfBirth1,... strings?
If you want to know which date is older just compare them:
You already have all the dates in your array. Just find the record that has the oldest date and print that record.
Question: why all the DateOfBirth1,... strings?
If you want to know which date is older just compare them:
Delphi Syntax (Toggle Plain Text)
var d1, d2: tDateTime; begin d1 := strToDate( '28/10/2007' ); d2 := strToDate( '29/10/2007' ); if d1 < d2 then writeln( 'correct: the 28th comes before the 29th' ) else writeln( 'error: apparently the 29th comes before the 28th...' ) end.
You already have all the dates in your array. Just find the record that has the oldest date and print that record.
![]() |
•
•
•
•
•
•
•
•
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



Linear Mode