Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help it keeps skipping readln's 1

Status
Not open for further replies.

kebab1701

Programmer
May 3, 2002
3
GB
hello im writing a program and some of the time i have to put a second read or readln cause it skips the first one this is usually after i have wrote something with a writeln or write can anyone help me?
 
The behavior you describe makes no sense. Why don't you post an example of what's going on?
 
There is one (very obscure and unlikely) possibility that occurs to me. Writeln accepts a variable amount of parameters in its list of things to write, which are passed on the stack. If you in any way mess up the stack (usually by inline assembly or calling procedures via pointers when the procedures aren't what the compiler was expecting) then writeln will occasionally handle the newly-mangled stack with less than its usual correctness. That can lead to problems with readln, though usually the problem is that a subsequent readln produces a "file not open for reading" error even though you were trying to read from the keyboard.
Maybe someone else knows more about the details of this.
 
The
Code:
read
procedure is not intended to read from the console (screen), you should use
Code:
readln
in stead.
If you do something like the following, it goes wrong:
Code:
write('Enter your first name: ');
read(s);
writeln('Your first name is: "',s,'"');
write('Enter your last name: ');
read(s); {This read is "skipped"}
writeln('Your last name is: "',s,'"');
The reason:
Code:
readln
waits until the return key is pressed before reading the console.
Code:
Read
on the other hand, doesn't wait and when there's nothing in the buffer, it returns an empty string.

Regards,
Bert Vingerhoets
vingerhoetsbert@hotmail.com
Don't worry what people think about you. They're too busy wondering what you think about them.
 
yes, everything I wrote applies only to readln. I have never used read.
 
thx guys i just replaced read with readlns and it works a treat now. Thx again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top