Lazarus readln doesn't read the variable

半世苍凉 提交于 2019-11-29 18:17:34

I noticed that the essential difference between the FPC Read() and ReadLn functions when used with standard input is not very well documented (in the Delphi documentation it can be read). Therefore, the answer to your question is the following.

According to the FPC documentation, CRLF, CR and LF, all are recognized as End-Of-Line (EOL) characters, so in the following EOL stands for any of those.

When calling Read(s) where s: string; the program waits for user input until an EOL is added to the input buffer. The input characters up until (but excluding) the EOL are removed from the buffer and passed to s. The EOL is left in the input buffer.

Next call to Read(s) will immediately recognize the EOL in the input buffer, and will not wait for user input. The EOL continues to remain in the buffer, and therefore no subsequent Read(s) will wait for user input.

The first call to ReadLn(s) after a Read(s) will also immediately detect the EOL in the buffer and not wait for user input.

When calling ReadLn(s) the program waits (as in previous example) for user input until an EOL is added to the input buffer. The input characters up until (but excluding) the EOL are removed from the buffer and passed to s. Then the EOL is removed from the buffer and thrown away.

Next call to ReadLn(s) will again stop and wait for user input.

In your code you are calling Read(n) in the very beginning of the execution. Then, in beiras() you are calling ReadLn() which detects the EOL in the buffer and therefore returns immediately, but also removes the EOL from the buffer. Subsequent calls to ReadLn() will now wait for user input.

The cure is iow to change the Read()n to a ReadLn(n) in the beginning of your program.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!