3

I have recently started coding in Delphi 7, mainly for school but also for personal entertainment. I have run into a problem I cannot solve. I wanted to make a console application, that asks you to input a couple of strings, such as your name and similar, so that the application may call you by your name later. However, I soon realized that having two "read();" commands for 2 different strings doesn't work for some reason, skipping the read command for the second string every time. Since I can't explain it perfectly, here is the simplest code I could think of that shows the problem:

program stringproblem;

{$APPTYPE CONSOLE}

uses
  SysUtils;

  var string1,string2:string;

begin
read(string1);
read(string2);
writeln(string1,string2);
readln;
readln;
end.

So the console opens, I get to write the value of string1, I input 'test', for example, but instead of letting me then input the value of string2, it skips that one, and just writes out 'test' in the console.

Why can't I input the values of two strings in an application? Why does writing the value of the first one automatically skip all others?

1 Answer 1

5

Instead of Read(string1) you should use Readln(string1). And likewise for the other read, and indeed whenever you wish to consume an entire line.

From the documentation for Read:

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.

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I have just tried doing so, changing both commands into readln, and it does work now. Thank you for your help, as stupid as my question was anyway.
The documentation links in the answer explain why Read won't get past the new line.
As an exercise, knowing what you know now, do you understand why your program has two Readln statements at the end?
I have been sitting here thinking about it and realized that I usually just used as many Readln statements as I needed to stop the program from closing. Were the 2 Readln commands needed in my program because the first Readln statement would return a zero-length string from the previous Read statement? I noticed that after correcting all Read statements into Readln statements, I would only need one Readln statement at the end of the program, since that one would no longer get a zero-length string from the end-of-line marker of the previous Read statement.
Thank you for your help, it really means a lot to someone who has barely scratched the surface of Delphi like me, and thanks for making me think about the Readln; situation, as I would have never thought about it without your comment. Is the registering of the end-of-line markers by subsequent Read statements considered a bug in Delphi, or were those Read statements actually supposed to register the end-of-line markers from previous lines on purpose? (as I see no function in it except for messing up variable values, and registering them as non-existent/zero-length...)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.