visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: Pascal :: decoding program


By: VB guy Canada  Date: 02/10/2003 00:00:00  English  Points: 300 Status: Answered
Quality : Excellent
I am writing a program that will write text to a file, read it, encode it, write it to a file, read it, decode it, and write it to a file.
Here is my program. Everything works until I get to the read2 procedure, then it freezes.


program menu (input,output);
uses
wincrt;
type
arraytype=array[1..25] of string;
var
p,ni,counter,flag1,flag2,flag3,flag4,flag5,flag6,i,j,k,c:integer;
ch:char;
l,line:arraytype;
n,message:string;
f:text;

procedure border;
begin

clrscr;
for i:=1 to 35 do
begin
gotoxy(i,1);
writeln('?');
gotoxy(71-i,1);
writeln('?');
gotoxy(i,20);
writeln('?');
gotoxy(71-i,20);
writeln('?');
gotoxy(1,i);
if i<=10 then writeln('?');
gotoxy(1,20-i);
if i<=10 then writeln('?');
gotoxy(70,i);
if i<=10 then writeln('?');
gotoxy(70,20-i);
if i<=10 then writeln('?');

for j:=1 to 3200 do
begin
gotoxy(1,1);
write;
c:=c-1;
gotoxy(32,1);
gotoxy(22,1);
write;
write;
write;
write;
end;
end;
end;



{ procedure write2;
begin
assign(f,'P:decoded');
rewrite(f);
for i:=1 to counter do
begin
writeln(f,l);
end;
close(f);
end; }




procedure read2;
begin
counter:=1;
assign(f,'p:encoded');
reset(f);
while not eof(f) do
begin
while not eoln(f) do
begin
readln(f,line[counter]);
counter:=counter+1;
end;
end;
close(f);
end;





{ procedure decode;
begin
read2;
for i:=1 to counter do
begin
k:=length(line);
for j:=1 to k do
begin
n:=copy(line,j,1);
ni:=ord(n[1]);
ni:=ni + 16;
n:=(chr(ni));
l:=concat(l,n);

end;
end;
write2;
end; }





procedure write1;
begin
assign(f,'P:encoded');
rewrite(f);
for i:=1 to counter do
begin
writeln(f,l);
end;
close(f);
end;





procedure read1;
begin

counter:=1;
assign(f,'p:original');
reset(f);
while not eof(f) do
begin
while not eoln(f) do
begin
readln(f,line[counter]);
counter:=counter+1;
end;
end;
close(f);
end;





procedure encode;
begin
read1;

for i:=1 to counter do
begin
k:=length(line);
for j:=1 to k do
begin
n:=copy(line,j,1);
ni:=ord(n[1]);
ni:=ni + 16;
n:=(chr(ni));
l:=concat(l,n);

end;
end;
write1;
end;




procedure add;
begin
clrscr;
writeln('Type in one sentence at a time.');
readln(message);
assign(f,'p:original');
append(f);
writeln(f,message);
close(f);
end;





procedure create;
begin
flag2:=1;
while flag2=1 do
begin
assign(f,'p:original');
rewrite(f);
close(f);
flag2:=2;
end;
end;










procedure mainmenu;
begin
flag1:=1;
while flag1=1 do
begin
border;
gotoxy(25,4);
writeln('*Main Menu*');
gotoxy(25,5);
writeln('[c]reate new message');
gotoxy(25,6);
writeln('[A]dd message');
gotoxy(25,7);
writeln('[E]ncode message.');
gotoxy(25,8);
writeln('[d]ecode');
gotoxy(25,9);
writeln('[p]rint message');
gotoxy(25,10);
readln(ch);
case(upcase(ch)) of
'A':add;
'C':create;
'E':encode;
'D':read2;
end;
end;
end;
begin
mainmenu;
end.



By: VGR Date: 02/10/2003 06:20:00 English  Type : Answer
indeed

it is entering an infiinite loop between the while not eof() and while not (eoln)

because the "encoded" data looks like : ('„„0uƒ„0s~„u~„', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '')

the second array element is EoLn...

so we jump infinitely from "we're not at eof" (which is true) and "we're at eoln" (which is true too)

what should you do when encountering eoln ? skip the line ?
By: VGR Date: 02/10/2003 06:25:00 English  Type : Comment
in fact, as "encoded" contains ONE line, we reach EoLn because you do a readln() in stead of a read(), ***but*** we're not at eof. This must be because you inserted a blank line after the encoded data.

won. There are two blank lines at the end.

***any*** empty line in "encoded" will produce the infinite loop condition.

What have you tried to acheive ? May I help ?
By: VGR Date: 03/10/2003 04:25:00 English  Type : Comment
Allo ?
By: VGR Date: 06/10/2003 19:34:00 English  Type : Comment
Hello ? Somebody there ?
By: VGR Date: 08/10/2003 17:06:00 English  Type : Comment
nice to read from you 8-)

Do register to be able to answer

EContact
browser fav
page generated in 109.488960 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page