Help variable reads too far THANX to anyone who tries to help 
Author Message
 Help variable reads too far THANX to anyone who tries to help

Program a6q3;
uses wincrt;

type
  book= record
           author: array[1..15] of char;
           title: array[1..30] of char;
           year:integer;
           price: real;
           end; {book}
Type
 libr = array[1..20] of book;

Var
  T:libr;
  l,p,count:integer;
  stop:char;

Procedure readauthor(i,counter:integer);
Begin
write('Please enter author:  ');
while not eoln do
begin
I:=I+1;
read(t[counter].author[I]);
write(t[counter].author[I]);
end;
readln;
end;

Procedure ReadTitle(i,counter:integer);
Begin
write('Please enter title:  ');
while not eoln do
begin
read(t[counter].title[i]);
i:=i+1;
end;
readln;
end;

Procedure Printauthor(i,counter:integer);
var
j:integer;

Begin

For J:= 1 to 15 do
begin

write(t[counter].author[j]);
end;
end;

Procedure Printtitle(J,COUNTER:integer);
var
i:integer;

Begin
write('          ');
For i:=1 to 30 do
begin
write(t[counter].title[i]);
end;
end;

Begin
stop:='y';
While stop='y' do
begin
count:=count+1;
ReadAuthor(l,count);

ReadTitle(p,count);
PrintAuthor(l,count);
{Printtitle(p,count); }
end;
end.

THis is my program now when I run it can anyone tell me why I would get the
t[counter].author variable to contain also the first letter of the title.
Help would be greatly appreciated.



Wed, 18 Jun 1902 08:00:00 GMT  
 Help variable reads too far THANX to anyone who tries to help

[snip Kevin's program]

Quote:
>THis is my program now when I run it can anyone tell me why I would get the
>t[counter].author variable to contain also the first letter of the title.
>Help would be greatly appreciated.

Hello Kevin,

I pointed this out in your earlier post about eoln. :-)
I was hoping you would be able to come up with a solution.

In the reply to your earlier post, I could only guess from
your read statement that "author" was an array of string and
provided a discussion of the problems you would encounter if
it was.  Now that you've included the record structure, I
can see my assumption was wrong.  Knowing the record
structure will help us provide an answer tailored to your
specific problem.

Before we discuss why your data entry procedures aren't
working, let's consider the fact that you have written a
program that will _never_ end!  You set stop = 'y' then
enter a while loop which will execute as long as stop = 'y',
but no where in the program is there anything that will
change the value of stop!  Et voila, you have created a
monster that will demand more data forever.  Well maybe not,
eventually your data entry procedures will start storing
information outside the "t" array, which will eventually
overwrite L, P, Count, and finally stop.  The odds are stop
will then contain a value other than 'y', so your program
will stop. ;-)

Another thing you are doing that is a potential hazard is
using variables before they are initialized.  For example,
you pass L and Count to the procedure without initializing
them.  Granted, you increment Count before each call, but
you assume that its initial value was zero.  The compiler
might save your {*filter*}by initializing all global values, but
this shortcoming is really going to raise its head when you
pass L by reference or start using local variables.  In the
case of L, you will increment it forever and it will never
be reset.  Of course this will simply appear as another bug
that you must identify and eliminate.

Now on to your problem. (It's about time, huh?).

First, the easiest solution is to read one string variable
per line.  For example:

VAR s: String;

    Write('Enter author: ');
    readln(s);

Now, to assign the contents of string "s" to Author
array[1..15] of Char;  Mmmm, could this be the reason for
the recent requests for ways to convert a string to an
array?  You might want to look at those threads. Meanwhile,
here's a different solution to the problem -

TYPE tAuthor = Array[1..15] of Char;
     tTitle  = Array[1..30] of Char;

TYPE Book = Record
       Author: tAuthor;
       Title : tTitle;
       Year  : Integer;
       Price : Real;
     End; {book}

TYPE tInput = RECORD
       Case Integer of
         1: (s: String[30]);
         2: (len1: Byte;
             Author: tAuthor);
         3: (len2: Byte;
             Title : tTitle);
       End;

The above variant record is designed so that author, title
and s will all occupy the same position in memory.  So now
we can have:

CONST
    Spaces = '                              '; { 30 spaces }

VAR temp: tInput;
BEGIN
    Writeln('Enter author: ');
    Readln(Temp.s);

    Temp.s := Temp.s + Spaces; { pad w/ trailing spaces }
    t[Count].Author := Temp.Author;

BTW, if you can define author and title as string[15] and
string[30] you could simple Readln(t[Count].Author);  

Before I leave I have to suggest that you make an
appointment with your teacher/instructor to discuss the
problems you are having and to walk through the code you've
written.  I'm not certain if your misunderstanding is a
result of insufficient design effort, a poor understanding
of the problem, or just a poor understanding of Pascal.
Regardless, a little time with your teacher will be the best
investment you could make.

    ...red



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Please Read!....I need Help!!! (@nd Try)

2. Thanx for the help

3. help needed. in parent/child relationship , thanx

4. Help variable reads t

5. Help : Anyone know how to read/write from/into Excel files

6. Help: One char too far!

7. HELP HELP (Tries in Pascal)

8. i need help on pascal anyone can help?

9. pascal help, can anyone help me to learn pascal

10. Bug (?) when trying to initialize double-typed variable

11. Anyone Tried to Use VBSQL.OCX?

12. Anyone tried Cascade/DB w/Delphi?

 

 
Powered by phpBB® Forum Software