Forum > Beginners

Readln not being executed after Read() (Solved)

(1/2) > >>

Hello everyone, first post.
I'm in the process of learning the pascal language with freepascal and encountered some weird behavior (to me) regarding readln() after read()
Was writing a small program to test the case statement, it is about roman digits:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Program RomanDigit;var i : integer; begin   write('i='); read(i);   case i of      1   : writeln('I');      10   : writeln('X');      50   : writeln('L');      100   : writeln('C');      500   : writeln('D');      1000 : writeln('M');   { else writeln('The number entered does not represent a roman digit'); }   otherwise writeln('The number entered does not represent a roman digit');   end;   writeln('Before');   readln; { don't know why this line is not being executed }   writeln('After');end. 
The last readln is not being executed.

The code works if I use readln(i) instead of read(i).
Also the code works if assign i:=1 (or any other number) instead of reading it from the keyboard.
Added printing 'Before' and 'After' for 'debugging' and they are printed normally, but readln is not being executed.
Maybe this is expected behavior, IDK, I'm still learning.
Any opinion to shed light on this situation is appreciated.
Thanks in advance.

That seems normal:

An user enters  number  and presses enter.

The read() reads the number, the readln() reads the enter.

Ah, I see.
It is a bit confusing, was expecting that readln to read the enter after writeln('Before'); and pause.
I guess I have to make use of another readln to pause the execution of the program before it exits, or just use readln(i) in the first place.


--- Quote from: ludan on September 17, 2022, 02:17:54 pm ---or just use readln(i) in the first place.

--- End quote ---
That one is the simplest.

Related to the console (command line) are terms such as standard input and standard output. The input is usually the keyboard. The output is the screen. In windowing systems, the output is the console (command line) window.

The keyboard has a data buffer that stores the characters that correspond to the keystrokes pressed by the user (input stream). The screen buffer (output stream) is bound to the screen. So the data at the console input is separated from the data at the console output.

The following procedures are used to retrieve data from the console input (keyboard): Read and ReadLn. The Write and WriteLn procedures are used to write data to the screen (console window). The "Ln" suffix stands for a line of text. The line of text consists of a string followed by a delimiter. They are different for different operating systems: CR (Mac), LF (Unix/Linux) or CRLF (Windows).

The Read procedure reads only as many characters as it can put in the variable. So you can pass several variables to it as arguments. The ReadLn procedure, on the other hand, reads the characters from the buffer together with the text delimiter. In both cases, characters are removed from console input after reading. Both of these routines can convert characters to base data types.

The Write procedure transfers the string that is displayed in the console window. The WriteLn procedure, on the other hand, adds a delimiter to the transferred characters. The result is that in the latter case, after writing text to the console window, the text cursor should move to the next line in the console window.

So if the WriteLn procedure writes a string of characters and puts a delimiter at the end, the result is that the text cursor in the console will move to the next line. And that's it. Attempting to read anything using the ReadLn routine will in no way result in reading this delimiter, because the console input is separate from the console output (ie the keyboard buffer from the screen buffer).

Please note that the above-mentioned procedures do not convert the contents of variables that are programmer-defined types.

Finally, one more thing: the question of using the ReadLn procedure call without any arguments in your code. If no arguments are given, this routine will only read the string delimiter from the console input, but only if the user presses the Enter key. It is usually used to stop the system from closing the console window so that the user has time to read the text on the screen. If the call was not in the program code, the program would exit immediately after processing the data and displaying the results, which means that the console window would also be closed.

The above description is deliberately simplified to explain only the most important issues related to the console (command line) operation.


[0] Message Index

[#] Next page

Go to full version