Hello again,

I have another assignment and need some help again.

This time, I have to display the contents of a record file, consisting of contact information for a list of people.

I have to create a text file with several lines of contact information- surname, firstname, email and tel number.

I have to use the newly created text file as input and create a record for each person and store in a file. After the record has been created, allow the user the option to see all records stored. The records should be displayed in the following order- Firstname, Surname, Tel number and email.

I completed the code for this program but it refuses to compile. Can someone please check my code and see where I went wrong and how to correct it?

Here is the code-

Program Directory (infile, Data, output);

{Program to display the contents of a record file, consisting of contact

 information for a list of people}

{This Program is expecting input from a text file, after which, the data

 will be both outputted to the screen and to a newly created text file}

TYPE

 REC_TYPE = RECORD {creates the variables for the Record File}

          FIRST_NAME : STRING [20];

          SURNAME : STRING [20];

          TEL : STRING [8];

          EMAIL : STRING [25];

 END; {End the creation of record variables}

 FILETYPE = FILE OF REC_TYPE; {Creates the Record File}

VAR

 Profile : REC_TYPE; {Stores the Record Variables}

 Data : FILETYPE; {File to store the record for each person's contact info}

 infile : TEXT; {indicates that the file is a text file}

 Choice : CHAR; {variable for Choice made}

 count : INTEGER;

{**************************************************************************}

PROCEDURE Display_Instructions; {Procedure without Parameters}

{This Procedure Displays Instructions On How To Use The Program}

BEGIN

 WRITELN ('This Program Displays Contact Information for a List of People.');

 WRITELN ('When the Program Commences, Please Choose One of the Options.');

 WRITELN ('If you choose YES, the information will be displayed.');

 WRITELN ('If you choose NO, the program will terminate.');

END; {End Procedure}

{***************************************************************************}

PROCEDURE Decision; {Procedure without Parameters}

BEGIN

 WRITELN ('Would You Like To View the Contact List?');

 WRITELN ('Y : Yes');

 WRITELN ('N : No');

END; {End Procedure}

{***************************************************************************}

PROCEDURE Contact_Data; {Procedure without Parameters}

{This Procedure allows for input and output of data to the text file}

BEGIN

 ASSIGN (infile, 'G:\Assignment2\Directory.txt');

 RESET (infile);

 ASSIGN (Data, 'G:\Assignment2\Data.txt');

 REWRITE (Data);

 WHILE NOT (EOF (infile)) DO

 BEGIN

            READLN (infile);

            {End WITH}

 END; {End WHILE}

 RESET (Data);

END; {End Procedure}

{***************************************************************************}

PROCEDURE Information; {Procedure without Parameters}

{This Procedure allows the data to be output to the screen}

BEGIN

 count := 0;

 WHILE NOT (EOF (Data)) DO

 BEGIN

      READLN (Data);

      WITH Profile DO

      BEGIN

           WRITELN ('Firstname,  , Surname, , Tel, , Email, ,');

           count := count + 1;

      END;

      END;

END;


{***************************************************************************}

BEGIN {Program Starts Here}

 Display_Instructions;

 WRITELN; {Allows a Line to be skipped}

 Decision;

 WRITELN; {Allows a Line to be skipped}

 WRITELN ('Please Enter Your Choice');

 READLN (Choice);

 WHILE (Choice = 'Y') OR (Choice = 'y') OR (Choice = 'N') OR (Choice = 'n') DO

 BEGIN

       IF (Choice = 'Y') OR (Choice = 'y') THEN

          Information

         ELSE IF (Choice = 'N') OR (Choice = 'n') THEN

                 WRITELN ('No Records Are Currently Being Displayed');

         READLN (Choice); {Prevents Eternal Looping of the Records}

       {End IF}

     {End ELSE}

 END; {End While}

 Contact_Data;

END. {End Program}

I have made some modifcations to the code since last night. However, when I compile, I keep getting a blank screen. Can someome help me out here?

Program Directory (infile, Data, output);

{Program to display the contents of a record file, consisting of contact

 information for a list of people}

{This Program is expecting input from a text file, after which, the data

 will be both outputted to the screen and to a newly created text file}

TYPE

 REC_TYPE = RECORD {creates the variables for the Record File}

          FIRST_NAME : STRING [20];

          SURNAME : STRING [20];

          TEL : STRING [8];

          EMAIL : STRING [25];

 END; {End the creation of record variables}

 FILETYPE = FILE OF REC_TYPE; {Creates the Record File}

VAR

 Profile : REC_TYPE; {Stores the Record Variables}

 Data : FILETYPE; {File to store the record for each person's contact info}

 infile : TEXT; {indicates that the file is a text file}

 Choice : CHAR; {variable for Choice made}

 count : INTEGER;

{**************************************************************************}

PROCEDURE Display_Instructions; {Procedure without Parameters}

{This Procedure Displays Instructions On How To Use The Program}

BEGIN

 WRITELN ('This Program Displays Contact Information for a List of People.');

 WRITELN ('When the Program Commences, Please Choose One of the Options.');

 WRITELN ('If you choose YES, the information will be displayed.');

 WRITELN ('If you choose NO, the program will terminate.');

END; {End Procedure}

{***************************************************************************}

PROCEDURE Decision; {Procedure without Parameters}

BEGIN

 WRITELN ('Would You Like To View the Contact List?');

 WRITELN ('Y : Yes');

 WRITELN ('N : No');

END; {End Procedure}

{***************************************************************************}

PROCEDURE Information; {Procedure without Parameters}

{This Procedure allows for input and output of data to the text file}

BEGIN

 ASSIGN (infile, 'G:\Assignment2\Directory.txt');

 RESET (infile);

 ASSIGN (Data, 'G:\Assignment2\Data.txt');

 REWRITE (Data);

 WHILE NOT (EOF (infile)) DO

 BEGIN

            READ (infile);

            WRITE (Data, Profile);

            {End WITH}

 END; {End WHILE}

 RESET (Data);

 count := 0;

 WHILE NOT (EOF (Data)) DO

 BEGIN

      READ (Data, Profile);

      WITH (Profile) DO

      BEGIN

           WRITELN ('Initial,  , Surname, , Tel, , Email, ,');

      END;

      count := count + 1;

 END;

END;

{***************************************************************************}

BEGIN {Program Starts Here}

 Display_Instructions;

 WRITELN; {Allows a Line to be skipped}

 Decision;

 WRITELN; {Allows a Line to be skipped}

 WRITELN ('Please Enter Your Choice');

 READLN (Choice);

 WHILE (Choice = 'Y') OR (Choice = 'y') OR (Choice = 'N') OR (Choice = 'n') DO

 BEGIN

       IF (Choice = 'Y') OR (Choice = 'y') THEN

          Information

         ELSE IF (Choice = 'N') OR (Choice = 'n') THEN

                 WRITELN ('No Records Are Currently Being Displayed');

         READLN (Choice); {Prevents Eternal Looping of the Records}

       {End IF}

     {End ELSE}

 END; {End While}

END. {End Program}

Ok, I used the debugger and nothing is being output. I just keep getting a blank screen. I modified the code again and came up with this. The error seems to be in the code somewhere, can someone just pinpoint the area where my code could be wrong?

Program Dir (input, infile, Data, output);

{Program to display the contents of a record file, consisting of contact

 information for a list of people}

{This Program is expecting input from a text file, after which, the data

 will be both outputted to the screen and to a newly created text file}

TYPE

 REC_TYPE = RECORD {creates the variables for the Record File}

          FIRST_NAME : STRING [20];

          SURNAME : STRING [20];

          TEL : STRING [8];

          EMAIL : STRING [25];

 END; {End the creation of record variables}

 FILETYPE = FILE OF REC_TYPE; {Creates the Record File}

VAR

 Profile : REC_TYPE; {Stores the Record Variables}

 Data : FILETYPE; {File to store the record for each person's contact info}

 infile : TEXT; {indicates that the file is a text file}

 Choice : CHAR; {variable for Choice made}

 count : INTEGER;

{**************************************************************************}

PROCEDURE Display_Instructions; {Procedure without Parameters}

{This Procedure Displays Instructions On How To Use The Program}

BEGIN

 WRITELN ('This Program Displays Contact Information for a List of People.');

 WRITELN ('When the Program Commences, Please Choose One of the Options.');

 WRITELN ('If you choose YES, the information will be displayed.');

 WRITELN ('If you choose NO, the program will terminate.');

END; {End Procedure}

{***************************************************************************}

PROCEDURE Decision; {Procedure without Parameters}

BEGIN

 WRITELN ('Would You Like To View the Contact List?');

 WRITELN ('Y : Yes');

 WRITELN ('N : No');

END; {End Procedure}

{***************************************************************************}

PROCEDURE Information; {Procedure without Parameters}

{This Procedure allows for input and output of data to the text file}

BEGIN

 ASSIGN (infile, 'G:\Assignment2\Direct.txt');

 RESET (infile);

 ASSIGN (Data, 'G:\Assignment2\Data.txt');

 REWRITE (Data);

 WHILE NOT (EOF (infile)) DO

 BEGIN

       WITH Profile DO

       BEGIN

       READ (infile, FIRST_NAME, SURNAME, TEL, EMAIL);

       END; {End WITH}

       WRITE (Data, Profile);

 END; {End WHILE}

 RESET (Data);

 WHILE NOT (EOF (Data)) DO

 count := 0;

 BEGIN

      READ (Data, Profile);

      WITH (Profile) DO

      BEGIN

           WRITE (FIRST_NAME, ' ', SURNAME, ' ', TEL, ' ', EMAIL, ' ');

      END;

      count := count + 1;

 END;

END;

{***************************************************************************}

BEGIN {Program Starts Here}

 Display_Instructions;

 WRITELN; {Allows a Line to be skipped}

 Decision;

 WRITELN; {Allows a Line to be skipped}

 WRITELN ('Please Enter Your Choice');

 READLN (Choice);

 WHILE (Choice = 'Y') OR (Choice = 'y') OR (Choice = 'N') OR (Choice = 'n') DO

 BEGIN

       IF (Choice = 'Y') OR (Choice = 'y') THEN

          Information

         ELSE IF (Choice = 'N') OR (Choice = 'n') THEN

                 WRITELN ('No Records Are Currently Being Displayed');

         READLN (Choice); {Prevents Eternal Looping of the Records}

       {End IF}

     {End ELSE}

 END; {End While}

 WRITELN (count);

END. {End Program}

With the debugger you can view your code as it executes line by line, to see where it goes wrong. Be sure to do that, and you will find the problem. Probably some variables do not have the expected value.

Ok, I used the debugger and nothing is being output. I just keep getting a blank screen. I modified the code again and came up with this. The error seems to be in the code somewhere, can someone just pinpoint the area where my code could be wrong?

Program Dir (input, infile, Data, output);
{Program to display the contents of a record file, consisting of contact information for a list of people}
{This Program is expecting input from a text file, after which, the data will be both outputted to the screen and to a newly created text file}

TYPE
  REC_TYPE = RECORD {creates the variables for the Record File}
          FIRST_NAME : STRING [20];
          SURNAME : STRING [20];
          TEL : STRING [8];
          EMAIL : STRING [25];
  END; {End the creation of record variables}

  FILETYPE = FILE OF REC_TYPE; {Creates the Record File}

VAR
  Profile : REC_TYPE; {Stores the Record Variables}
  Data : FILETYPE; {File to store the record for each person's contact info}
  infile : TEXT; {indicates that the file is a text file}
  Choice : CHAR; {variable for Choice made}
  count : INTEGER;

{**************************************************************************}

PROCEDURE Display_Instructions; {Procedure without Parameters}
{This Procedure Displays Instructions On How To Use The Program}
BEGIN
  WRITELN ('This Program Displays Contact Information for a List of People.');
  WRITELN ('When the Program Commences, Please Choose One of the Options.');
  WRITELN ('If you choose YES, the information will be displayed.');
  WRITELN ('If you choose NO, the program will terminate.');
END; {End Procedure}

{***************************************************************************}
PROCEDURE Decision; {Procedure without Parameters}
BEGIN
  WRITELN ('Would You Like To View the Contact List?');
  WRITELN ('Y : Yes');
  WRITELN ('N : No');
END; {End Procedure}

{***************************************************************************}
PROCEDURE Information; {Procedure without Parameters}
{This Procedure allows for input and output of data to the text file}
BEGIN
  ASSIGN (infile, 'G:\Assignment2\Direct.txt');
  RESET (infile);
  ASSIGN (Data, 'G:\Assignment2\Data.txt');
  REWRITE (Data);
  WHILE NOT (EOF (infile)) DO
  BEGIN
       WITH Profile DO
       BEGIN
         READ (infile, FIRST_NAME, SURNAME, TEL, EMAIL);
       END; {End WITH}
       WRITE (Data, Profile);
  END; {End WHILE}
  RESET (Data);  <<=== You must to [B]close[/B] [U]Data[/U] before [B]Reset[/B] than to
  force flush data
  WHILE NOT (EOF (Data)) DO
  count := 0;  <<=== Atention here. This command must to be before
  the [B]While[/B]. You'll get a infinte loop when [U]Data[/U] have data becose you'll
  never reach end-of-file after Reset command (except if [U]Data[/U] is empty)
  BEGIN
      READ (Data, Profile);
      WITH (Profile) DO
      BEGIN
           WRITE (FIRST_NAME, ' ', SURNAME, ' ', TEL, ' ', EMAIL, ' ');
      END;
      count := count + 1;
  END;
END;

{***************************************************************************}
BEGIN {Program Starts Here}
  Display_Instructions;
  WRITELN; {Allows a Line to be skipped}
  Decision;
  WRITELN; {Allows a Line to be skipped}
  WRITELN ('Please Enter Your Choice');
  READLN (Choice);
  WHILE (Choice = 'Y') OR (Choice = 'y') OR (Choice = 'N') OR (Choice = 'n') DO
  BEGIN
       IF (Choice = 'Y') OR (Choice = 'y') THEN
          Information
       ELSE IF (Choice = 'N') OR (Choice = 'n') THEN
          WRITELN ('No Records Are Currently Being Displayed');
       READLN (Choice); {Prevents Eternal Looping of the Records}
       {End IF}
     {End ELSE}
  END; {End While}
  WRITELN (count);
END. {End Program}

I put the comments into the code in red color. Make the changes e see if your problem is solved.

[]s

Hi,

Does your input file contain each field in a separate line or each record in a single line with some kind of delimiter (comma, semicolon, tab, space) ? If the former then you can't read it using READ (infile, FIRST_NAME, SURNAME, TEL, EMAIL); you should use separate "readln"s for each field. Your code only works if the input file isn't delimited at all neither for fields, nor records.

Loren Soth

This article has been dead for over six months. Start a new discussion instead.