PDA

View Full Version : Rudimentary Explaination Needed (Read Command)



Xorcist
13-04-2005, 03:38 AM
Why is it in Delphi 5.0 that if you run the following code:
(where iVar is an integer)

Write('Please Enter An Integer: ');
Read(iVar);
Writeln('The Integer You Entered Was: ', iVar);
Readln;

The program ignores the Readln; statement. Almost as if the EOLN entered for the Read gets passed onto the Readln. If this is the case... is there anyway to prevent this from happening? I find it hard to believe that this input would in some was be buffered up and held, then used by the Readln later on.

cairnswm
13-04-2005, 04:47 AM
This is correct. Read:

As the help says:

Read reads all characters up to, but not including, the next end-of-line marker or until Eof(F) becomes true; it does not skip to the next line after reading. If the resulting string is longer than the maximum length of the string variable, it is truncated.
After the first Read, each subsequent Read sees the end-of-line marker and returns a zero-length string.
Use multiple Readln calls to read successive string values.

So the end-of-lin marker is still in the buffer and read immediatly by ReadLn.

Xorcist
15-04-2005, 02:45 AM
Okay, simple enough. But second question... is there a way to do a system("PAUSE") in Delphi? I've done it in C, so I'm hoping there is some equivalent, other than readln.

cairnswm
15-04-2005, 05:57 AM
Not sure exactly what you want - Do you want a sleep function or wit for the next input from the user?

Xorcist
17-04-2005, 04:10 AM
Well I was using Readln to pause for "ENTER" to close out the application. But seeing as a Read before a Readln basically voids that Readln, the app was just closing without pausing... so I needed two Readlns to pause. What I want is a single standard way to pause at the end of an app, that isn't dependant on what has come before it in the code...

Update: it looks like if I Reset(input) before I use Readln to pause, it eliminates the EOLN that was getting passed on. So I should be able to create a procedure that does just that, and name it Pause. Of course, that could cause trouble if used somewhere other than the end of the application, as I'm basically flushing the input buffer... but not handling for any data that might be queued up.