Board index » delphi » Help, I can't figure this program out (PASCAL)

Help, I can't figure this program out (PASCAL)

Antony Ellis <"antony "@helm.demon.co.uk> wrote:

Quote
>(1)
>*************************************************************
>  For ch := 'A' to 'Z' do
>   Begin
>    Write(ch,store[ch]);
>    If store[ch] < 75 then
>      Begin
>      For Star := 1 to Store[ch] do
>      Write(OutFile,' *');
>      End   {if}
>    Else
>      Begin
>      For Star := 1 to 75 do
>        Write(OutFile,' *');
>      Writeln(OutFile,'. . .');
>      End; {else}

{>    close(outFile);  Move it to below}

- Show quoted text -

Quote
>   End;  {for}
    close(outfile);
>end;
>*********************************************************
>Begin  {Main program}
>   CountWord := 0;
>   CountLetter := 0;
>   AvWordLength := 0;
>   AVowelPercent := 0;
>   EVowelPercent := 0;
>   IVowelPercent := 0;
>   OVowelPercent := 0;
>   UVowelPercent := 0;
>   StartofWord := False;
>(2)
>**************************************************************
>   Writeln('Please type file name and press return=>');
>   Read(Filename);

{>   Assign(InFile,'Filename');} {remove quotes}
    Assign(Infile, Filename);

Quote
>   Reset(InFile);

Brad
 

Re:Help, I can't figure this program out (PASCAL)


This program should let me type a Path and file name, then it
should read the file for the following:
        A. Number of words
        B. Average word length
        C. Percentage of vowels
        C. Frequency count (i.e. No. of 'A' ********  
                                        'B' ****
                                         .
                                         etc. etc
                                         .
                                        'Z' ****************

and then write the result on to a file called wordout.txt.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
I HAVE TWO PROBLEMS

1.      When I write the path and file name the program says it
can't find the file, but it works if I hard-code it in the
program-Assign(Infile,'Dir\file').
Q. What am I doing wrong?

2. The part of the program that write the result of the array
to wordout.txt don't work. It also said it can't close the file as
it not open.
Q. What am I doing wrong?

Below, I have grouped the area where I think things are not right.
THANKS FRIENDS.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

program  Textfile;{Input,Output text}

Type
AtoZ = array ['A'..'Z'] of Integer;
var
  Store : AtoZ ;
  InFile,OutFile : Text;
  CountWord,Countletter, Star : Integer;
  AvWordLength : Real;
  ch : char;
  AVowelPercent : Real;
  EVowelPercent : Real;
  IVowelPercent : Real;
  OVowelPercent : Real;
  UVowelPercent : Real;
  StartofWord : Boolean;
  Filename : string;

procedure ProcessCharacter;
  begin
  If ch in ['A'..'Z','0'..'9'] then {}
    begin
     If not StartofWord then
      begin
       Store[ch] := Store[ch] + 1;
       Countletter := Countletter + 1;
      end
    end;
  end; {processcharacter}

procedure ProcessWord;
  begin
  If ch in ['A'..'Z','0'..'9'] then {}
    begin
     If not StartofWord then
       begin
         CountWord := CountWord + 1;
         StartofWord := true;
       end
    end
   end;

procedure DoStats;
  begin
   AvWordLength := Countletter / CountWord;
   AVowelPercent := Store['A'] / CountLetter * 100;
   EVowelPercent := Store['E'] / CountLetter * 100;
   IVowelPercent := Store['I'] / CountLetter * 100;
   OVowelPercent := Store['O'] / CountLetter * 100;
   UVowelPercent := Store['U'] / CountLetter * 100;
  end;

procedure DoResults;
begin
   Assign(OutFile,'a:\WordOut.TxT');
   Rewrite(OutFile);
   writeln(OutFile, 'Number of words ', CountWord);

   writeln(OutFile,'Average word length ', AvWordLength:5:3,'
Characters');

   writeln(OutFile,'A ',AVowelPercent:5:3,'%');
   writeln(OutFile,'E ',EVowelPercent:5:3,'%');
   writeln(OutFile,'I ',IVowelPercent:5:3,'%');
   writeln(OutFile,'O ',OVowelPercent:5:3,'%');
   writeln(OutFile,'U ',UVowelPercent:5:3,'%');
(1)
*************************************************************
  For ch := 'A' to 'Z' do
   Begin
    Write(ch,store[ch]);
    If store[ch] < 75 then
      Begin
      For Star := 1 to Store[ch] do
      Write(OutFile,' *');
      End   {if}
    Else
      Begin
      For Star := 1 to 75 do
        Write(OutFile,' *');
      Writeln(OutFile,'. . .');
      End; {else}
    close(outFile);
   End;  {for}
end;
*********************************************************
Begin  {Main program}
   CountWord := 0;
   CountLetter := 0;
   AvWordLength := 0;
   AVowelPercent := 0;
   EVowelPercent := 0;
   IVowelPercent := 0;
   OVowelPercent := 0;
   UVowelPercent := 0;
   StartofWord := False;
(2)
**************************************************************
   Writeln('Please type file name and press return=>');
   Read(Filename);

   Assign(InFile,'Filename');
   Reset(InFile);
***********************************************************
   While not eof (InFile) do
    Begin
     Read(InFile,ch);
     ch := upcase(ch);
     ProcessCharacter;
     ProcessWord;
    end;

   Close(InFile);
   DoStats;
   DoResults;

End.

Re:Help, I can't figure this program out (PASCAL)


In article <30BA489E....@helm.demon.co.uk>
           "antony "@helm.demon.co.uk "Antony Ellis" writes:

Quote
>...
>1.      When I write the path and file name the program says it
>can't find the file, but it works if I hard-code it in the
>program-Assign(Infile,'Dir\file').
>Q. What am I doing wrong?

>2. The part of the program that write the result of the array
>to wordout.txt don't work. It also said it can't close the file as
>it not open.
>Q. What am I doing wrong?

>Below, I have grouped the area where I think things are not right.
>THANKS FRIENDS.
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>   Assign(InFile,'Filename');

    Assign(InFile, Filename );
Quote

Above will help, I think.  
I suspect your "Reply" address is malformed.
--
Dr John Stockton, Surrey, UK

Re:Help, I can't figure this program out (PASCAL)


Antony Ellis <"antony "@helm.demon.co.uk> wrote:

Quote
>procedure DoResults;
>begin
>   Assign(OutFile,'a:\WordOut.TxT');

This is ok, but neater is perhaps

procedure DoResults;

CONST
  OutFileName = 'A:\WORDOUT.TXT';

BEGIN
  Assign(OutFile,OutFileName);
  etc.

Note that OutFile may be local.

Quote
>  For ch := 'A' to 'Z' do
>   Begin
>    Write(ch,store[ch]);
>    code omitted
>    close(outFile);
>   End;  {for}

Move the close(outFile) to below the End; {for}. As it stands
now, the file is opened, you process 'A', close the file, process
'B' with a closed output file --> bang!

Also, shouldn't Write(ch,store[ch]); be Write(OutFile,ch,store[ch]);??

Quote
>   Writeln('Please type file name and press return=>');
>   Read(Filename);

>   Assign(InFile,'Filename');

Here, you're trying to open a file with the name Filename.
Do this instead: Assign(InFile,FileName) (i.e. no quotes!)
and will be fine.

Good luck
... DH/PP

Other Threads