read/readln procedures 
Author Message
 read/readln procedures

Hi,
 I have written my own readln procedure, and made it into a unit, but i
cannot seem to pass the variable back to the main program. Does anyone know
how the original read/readln procedures were written, or can anyone give me
advice on how to solve my problem?

Any help appreciated.
Thanks



Wed, 18 Jun 1902 08:00:00 GMT  
 read/readln procedures

Quote:

>Hi,
> I have written my own readln procedure, and made it into a unit, but i
>cannot seem to pass the variable back to the main program. Does anyone know
>how the original read/readln procedures were written, or can anyone give me
>advice on how to solve my problem?

You do not define your problem well enough. Just try to read the above
forgetting all what you know about the problem and think if you could
understand what the problem is?

You cannot duplicate the behavior of read/readln. You cannot write
procedures with variable numbers and types of parameters.

Please give some code and say what the problem is exactly. We do not know
your code, only you know it.

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 read/readln procedures
Here's the code for my procedure (sorry about not including it before):

procedure preadln(sendto:string);
var c:char;
    rtnpress:boolean;
begin
 sendto:='';
 repeat
  c:=readkey;
  if c=#13 then rtnpress:=true else rtnpress:=false;
  if c>=#32 then
   begin
    write(c);
    sendto:=sendto+c;
   end;
  if (c=#8) and (length(sendto)>0) then
   begin
    delete(sendto, length(sendto), 1);
    gotoXY(WhereX-1, WhereY);
    write(#32);
    gotoXY(WhereX-1, WhereY);
   end;
 until rtnpress;
 writeln;
end;

Whats happening is, i call PREADLN from another procedure (e.g.
preadln(command) ), but i cannot get the procedure to place the value in the
variable SENDTO into the passed variable COMMAND. Is there anyway to do
this?



Wed, 18 Jun 1902 08:00:00 GMT  
 read/readln procedures

Quote:

>Here's the code for my procedure (sorry about not including it before):

>procedure preadln(sendto:string);

Here is the problem. The parameter is a value parameter. If does not
pass any data from the procedure to the caller. Make it a variable
parameter.  

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 read/readln procedures
James O'Gorman heeft geschreven in bericht

<snip>
Quote:
>Whats happening is, i call PREADLN from another procedure (e.g.
>preadln(command) ), but i cannot get the procedure to place the value
in the
>variable SENDTO into the passed variable COMMAND. Is there anyway to
do
>this?

Use: procedure preadln(var sendto:string);
if you want sendto to be an output-parameter

Huub.



Wed, 18 Jun 1902 08:00:00 GMT  
 read/readln procedures
Thanks for the help guys. Tere was just one more thing i was wondering
about: if i have global variable cmd:string[79] and i have procedure
preadln(var sendto:string), then it says the types are incompatible, yet i
cannot have procedure preadln(var sendto:string[79]). Is there any way
around this, or do i have to have cmd as just a string?

Thanks again.


Quote:
> Hi,
>  I have written my own readln procedure, and made it into a unit, but i
> cannot seem to pass the variable back to the main program. Does anyone
know
> how the original read/readln procedures were written, or can anyone give
me
> advice on how to solve my problem?

> Any help appreciated.
> Thanks



Wed, 18 Jun 1902 08:00:00 GMT  
 read/readln procedures


Quote:
> Thanks for the help guys. Tere was just one more thing i was wondering
> about: if i have global variable cmd:string[79] and i have procedure
> preadln(var sendto:string), then it says the types are incompatible,
yet i
> cannot have procedure preadln(var sendto:string[79]). Is there any way
> around this, or do i have to have cmd as just a string?

> Thanks again.



> > Hi,
> >  I have written my own readln procedure, and made it into a unit,
but i
> > cannot seem to pass the variable back to the main program. Does
anyone
> know
> > how the original read/readln procedures were written, or can anyone
give
> me
> > advice on how to solve my problem?

> > Any help appreciated.
> > Thanks

Create a TYPE for the string[79], and use that in the declaration of
both...

Sent via Deja.com http://www.deja.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 read/readln procedures

Quote:

>Thanks for the help guys. Tere was just one more thing i was wondering
>about: if i have global variable cmd:string[79] and i have procedure
>preadln(var sendto:string), then it says the types are incompatible, yet i
>cannot have procedure preadln(var sendto:string[79]). Is there any way
>around this, or do i have to have cmd as just a string?

There are two ways to do it:

1) Create a type like Type string79=String[79] and use it in the
procedure.

2) In TP use open string parameters. That is use key word "openstring"
(or what a compiler directive normal string)

There is a third way of turning the strict string checking off with $V-,
but it is extremely dangerous as if the user exceeds the 79 characters
the user will overwrite other variables. ($V- is good only in cases
where the original length of the string can never increase, like
capitalizing strings, removing trailing spaces etc.)

The open strings are safe as TP passes the actual size of the string as
a hidden parameter and the subroutine can then use it to check that it
is not exceeded.

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 read/readln procedures
I tried the type sender=string[79]; but if i only use this in the procedure
parameter, and still have cmd:string[79]; it doesn't work (however i have
them both as the type sender=string[79] now so that part works, thanks!)

I was not too clear on what you meant by the open string parameter though,
and was curious to try it. Could you please explain it in a little more
detail? Thanks once again! (This should be the last time I have to ask
anything on this subject!)


Quote:


> >Thanks for the help guys. Tere was just one more thing i was wondering
> >about: if i have global variable cmd:string[79] and i have procedure
> >preadln(var sendto:string), then it says the types are incompatible, yet
i
> >cannot have procedure preadln(var sendto:string[79]). Is there any way
> >around this, or do i have to have cmd as just a string?

> There are two ways to do it:

> 1) Create a type like Type string79=String[79] and use it in the
> procedure.

> 2) In TP use open string parameters. That is use key word "openstring"
> (or what a compiler directive normal string)

> There is a third way of turning the strict string checking off with $V-,
> but it is extremely dangerous as if the user exceeds the 79 characters
> the user will overwrite other variables. ($V- is good only in cases
> where the original length of the string can never increase, like
> capitalizing strings, removing trailing spaces etc.)

> The open strings are safe as TP passes the actual size of the string as
> a hidden parameter and the subroutine can then use it to check that it
> is not exceeded.

> Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 read/readln procedures

Quote:

> I tried the type sender=string[79]; but if i only use this in the procedure
> parameter, and still have cmd:string[79]; it doesn't work (however i have
> them both as the type sender=string[79] now so that part works, thanks!)

> I was not too clear on what you meant by the open string parameter though,
> and was curious to try it. Could you please explain it in a little more
> detail? Thanks once again! (This should be the last time I have to ask
> anything on this subject!)

Please look up $V and $P in your online help.

The following code

program test;
type X=String[1];
procedure bla(var s: X); begin end;
var s: String[1];
begin
  bla(s)
end.

is only valid with $V- because the compiler thinks X and String[1] are
different types.



Wed, 18 Jun 1902 08:00:00 GMT  
 read/readln procedures

Quote:

>I tried the type sender=string[79]; but if i only use this in the procedure
>parameter, and still have cmd:string[79]; it doesn't work (however i have
>them both as the type sender=string[79] now so that part works, thanks!)

The first lesson in Pascal programming is: define and use types. The
second lesson is: define and use types. The third lesson is: define
and use types. That is define only variables that are of some explicit
type, no some "var a:array[1..100] of something", or "s:string[nn];" but:
"type TArr=array[1..100] of something; var a:Tarr;" and "type
Tstr=string[nn]" and "var s:Tstr".

I wonder why Pascal even allows the sloppy direct definition of
variables without explicit type.

Also sender is a very confusing name as type name.

Quote:

>I was not too clear on what you meant by the open string parameter though,
>and was curious to try it. Could you please explain it in a little more
>detail? Thanks once again! (This should be the last time I have to ask
>anything on this subject!)

You define open string parameters with keyword openstring. The nice
thing with open strings is that the procedure handles the string
properly no matter what the size of the actual parameter is. Lets say
you add a character to the string. If the actual parameter already uses
its fills size then no character should be added. Open string parameters
can handle this automatically

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 read/readln procedures


Quote:

>Please look up $V and $P in your online help.

>The following code

>program test;
>type X=String[1];
>procedure bla(var s: X); begin end;
>var s: String[1];
>begin
>  bla(s)
>end.

>is only valid with $V- because the compiler thinks X and String[1] are
>different types.

And why not toss in a few gotos also when you are at it?

The proper way is to define

var s:X

$V- is EXTREMELY dangerous. What would happen if one increased the
length in the string in type X? Also $P+ can be confusing. It might be
better just to define the parameters as openstrings.

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 read/readln procedures

Quote:

> >The following code

> >program test;
> >type X=String[1];
> >procedure bla(var s: X); begin end;
> >var s: String[1];
> >begin
> >  bla(s)
> >end.

> >is only valid with $V- because the compiler thinks X and String[1] are
> >different types.

> And why not toss in a few gotos also when you are at it?

> The proper way is to define

> var s:X

> $V- is EXTREMELY dangerous. What would happen if one increased the
> length in the string in type X? Also $P+ can be confusing. It might be
> better just to define the parameters as openstrings.

> Osmo

You are right, of course, but I wanted to show what the compiler accepts
when in $V- mode.
I use almost always "String" with Freepascal ($H+) - then Strings are
"AnsiStrings" and don't have this annoying 255 character limit and there
is no problem with var-parameters at all.
But with TP/BP the best choice is openstring IMHO.

Wolf



Wed, 18 Jun 1902 08:00:00 GMT  
 read/readln procedures
Hi James,

if you want preadln(...) to modify the sendto string, use:

procedure preadln(var sendto: string);

This will tell the compiler to pass a reference to the original variable
and thus make the global variable editable to the procedure.
Please note though, that you will have to pass a variable, just what
another function returns won't be enought. This will NOT work:
preadln(command(...));

HTH

Sebastian

Quote:

> Here's the code for my procedure (sorry about not including it before):

> procedure preadln(sendto:string);
> var c:char;
>     rtnpress:boolean;
> begin
>  sendto:='';
>  repeat
>   c:=readkey;
>   if c=#13 then rtnpress:=true else rtnpress:=false;
>   if c>=#32 then
>    begin
>     write(c);
>     sendto:=sendto+c;
>    end;
>   if (c=#8) and (length(sendto)>0) then
>    begin
>     delete(sendto, length(sendto), 1);
>     gotoXY(WhereX-1, WhereY);
>     write(#32);
>     gotoXY(WhereX-1, WhereY);
>    end;
>  until rtnpress;
>  writeln;
> end;

> Whats happening is, i call PREADLN from another procedure (e.g.
> preadln(command) ), but i cannot get the procedure to place the value in the
> variable SENDTO into the passed variable COMMAND. Is there anyway to do
> this?



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

 Relevant Pages 

1. readln inside a procedure

2. Differences between READ and READLN

3. Differences between READ and READLN

4. Read & Readln

5. Beginner needs help....How do you make readln act for a password...read

6. Newbie...help on ReadLn...Read data

7. Reading files with Readln?

8. Reading files with Readln:

9. Procedure wont read in file

10. Reading vars from other procedures?

11. read/write access Procedure.

12. Calling a procedure within a procedure

 

 
Powered by phpBB® Forum Software