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 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
Reply
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
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: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Help Needed Straight Away..

  #11  
Nov 3rd, 2007
Hey there. Here is your code more carefully formatted and with some commentary.
  1. program Project1;
  2. {$APPTYPE CONSOLE}
  3. uses
  4. SysUtils;
  5.  
  6. type
  7. Tperson = record
  8. Name: String;
  9. Dateofbirth: TDateTime;
  10. end;
  11.  
  12. var
  13. s: string;
  14. FileA: textfile;
  15. datestring: string;
  16. personarray: array [1..5] of Tperson; // very good
  17. personinfo: Tperson;
  18. I: Integer;
  19.  
  20.  
  21. begin
  22. // You only need to set this global value once
  23. ShortDateFormat := 'd/mm/yyyy';
  24.  
  25. // Good
  26. Assign(FileA,'PeopleInformation.txt');
  27.  
  28. // The following reads each line of the file but
  29. // does nothing with it...
  30. Reset(FileA);
  31. while not EOF(FileA) do
  32. begin
  33. Readln(FileA,s);
  34. // See note 1
  35. end;
  36. close(FileA);
  37.  
  38. // The following is a loop that properly reads each
  39. // record from file. However, like the first loop's
  40. // s:string variable, the information is never used;
  41. // it is forgotten the next time through the loop.
  42. // See note 2
  43. Reset (FileA);
  44. while not EOF(FileA) do
  45. begin
  46. Readln (FileA,personinfo.name);
  47. Readln (FileA,datestring);
  48. personinfo.Dateofbirth:=strToDate(datestring);
  49. Readln ( FileA );
  50. end;
  51.  
  52. // Here you try to print a variable you never initialized.
  53. // See note 3
  54. writeln (personarray[1].name, personarray[1].Dateofbirth);
  55.  
  56. // Wait for the user to press ENTER before terminating
  57. readln;
  58.  
  59. 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 1
coment 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.something
with
personarray[ i ].something

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
writeln( myperson.Name, myperson.DateOfBirth );
it does just that, first printing

then printing
00000000E+0000
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.
Reply With Quote  
Join Date: Nov 2007
Posts: 10
Reputation: ickle2 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
ickle2 ickle2 is offline Offline
Newbie Poster

Re: Help Needed Straight Away..

  #12  
Nov 3rd, 2007
thanks for the effort you have put in for helping me its really appreciated
when you say : "TDateTime is stored as a floating point double"

what is that
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
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: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Help Needed Straight Away..

  #13  
Nov 3rd, 2007
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.
Last edited by Duoas : Nov 3rd, 2007 at 5:46 pm.
Reply With Quote  
Join Date: Nov 2007
Posts: 10
Reputation: ickle2 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
ickle2 ickle2 is offline Offline
Newbie Poster

Re: Help Needed Straight Away..

  #14  
Nov 3rd, 2007
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:
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.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
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: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Help Needed Straight Away..

  #15  
Nov 3rd, 2007
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 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.

  1. uses SysUtils;
  2. var
  3. d: tDateTime;
  4. s: string;
  5. begin
  6. write( 'Please enter a date as M/D/Y: ' );
  7. readln( s );
  8. try
  9. d := strToDate( s )
  10. except
  11. writeln( 'You did not enter a valid date.' );
  12. halt
  13. end;
  14. ShortDateFormat := 'mmmm d, yyyy';
  15. writeln( 'The date you entered was ', dateToStr( d ) )
  16. end.
Reply With Quote  
Join Date: Nov 2007
Posts: 10
Reputation: ickle2 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
ickle2 ickle2 is offline Offline
Newbie Poster

Re: Help Needed Straight Away..

  #16  
Nov 4th, 2007
thanks
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
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: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Help Needed Straight Away..

  #17  
Nov 4th, 2007
Glad to have been of help.
Reply With Quote  
Join Date: Nov 2007
Posts: 10
Reputation: ickle2 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
ickle2 ickle2 is offline Offline
Newbie Poster

Re: Help Needed Straight Away..

  #18  
Nov 4th, 2007
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:
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.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
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: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Help Needed Straight Away..

  #19  
Nov 4th, 2007
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:
  1. var d1, d2: tDateTime;
  2. begin
  3. d1 := strToDate( '28/10/2007' );
  4. d2 := strToDate( '29/10/2007' );
  5. if d1 < d2
  6. then writeln( 'correct: the 28th comes before the 29th' )
  7. else writeln( 'error: apparently the 29th comes before the 28th...' )
  8. end.

You already have all the dates in your array. Just find the record that has the oldest date and print that record.
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 1:17 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC