Recent

Author Topic: Readln in lazarus pascal not working  (Read 2156 times)

SW48

  • New Member
  • *
  • Posts: 36
Readln in lazarus pascal not working
« on: November 04, 2019, 06:40:13 pm »
When I

writeln('enter number');
readln(number);
writeln('enter number2');
readln(number2);

it asks for the number and I type it and it reads it upon hitting return as it should.

The second writeln shows on screen but when I type a number it doesn't show on the keyboard unless I type it a second time.

Any idea why this is happening?

winni

  • Hero Member
  • *****
  • Posts: 2206
Re: Readln in lazarus pascal not working
« Reply #1 on: November 04, 2019, 07:15:37 pm »
Hi!

Your terminal window or cmd window is not high enough. Expand it to full screen.

Or:

Code: Pascal  [Select][+][-]
  1. write('enter number:  ');
  2. readln(number);
  3. write('enter number2:  ');
  4. readln(number2);
  5.  

Winni

SW48

  • New Member
  • *
  • Posts: 36
Re: Readln in lazarus pascal not working
« Reply #2 on: November 05, 2019, 12:15:11 am »
Thank you very much for the response but neither of them worked.

Do you have anything else you can think of for me to try?

jamie

  • Hero Member
  • *****
  • Posts: 4199
Re: Readln in lazarus pascal not working
« Reply #3 on: November 05, 2019, 12:39:03 am »
what OS are you on and can you please post the complete lines here ?

Btw, if that is the last line of the program and you are in console mode it seems, you need to place a readln as the last function so the console screen will stay open and wait for you to hit enter.
The only true wisdom is knowing you know nothing

SW48

  • New Member
  • *
  • Posts: 36
Re: Readln in lazarus pascal not working
« Reply #4 on: November 05, 2019, 12:45:17 am »
Thanks.

Windows 10 version 1903

This happens with every keyboard input after the first one which works fine.  The rest of them I have to type the number or letter a second time before it shows up on the screen and can be read in.

Actual Code is:

Write('Choose the Home team by number and hit enter  ');
  Readln(teamnumber);
  HomeTeamAcronym := teamacronym[teamnumber];
  t1 := teamnamelist[teamnumber];
  Write('Choose the Visiting team by number and hit enter  ');
  Readln(teamnumber);
  VisitingTeamAcronym := teamacronym[teamnumber];   

winni

  • Hero Member
  • *****
  • Posts: 2206
Re: Readln in lazarus pascal not working
« Reply #5 on: November 05, 2019, 12:58:04 am »
Hi!

Yeah, that's the trick - what Jamie said about the last line of code.

In the tradition of the 80s the last three lines of code have to look like this:

Code: Pascal  [Select][+][-]
  1. writeln ('Thats all folks ......');
  2. readln;
  3. end.

Buena note
Winni

SW48

  • New Member
  • *
  • Posts: 36
Re: Readln in lazarus pascal not working
« Reply #6 on: November 05, 2019, 01:12:49 am »
I have the code you show here as my last 3 lines.

What I am referring to is early in a 10,000 line program where I am inputting what teams I want to use for a game.

The first writeln and readln the input number shows up on the screen on the first keystroke.

The second writeln and readlin I have to press the number on the keyboard twice for it to show up on the screen.

Now if I press 1 for team 1 and then hit enter twice then press 2 for team 2.  The 2 does show up on the screen on the first press.

But this code is nowhere near the end of the program.  It is at the very beginning and there are keyboard inputs throughout the entire program. 

SW48

  • New Member
  • *
  • Posts: 36
Re: Readln in lazarus pascal not working
« Reply #7 on: November 05, 2019, 01:19:18 am »
I think I solved it.

If I right click in the upper left corner of the command prompt and select "properties" then "Use Legacy Console" it works properly with my pascal programs.

Thanks everyone for giving me ideas on the fix.

SW48

  • New Member
  • *
  • Posts: 36
Re: Readln in lazarus pascal not working
« Reply #8 on: November 05, 2019, 01:22:26 am »
I spoke too soon.  It still doesn't work.

I have to type 1 then enter then either enter or type 2 twice...sigh

440bx

  • Hero Member
  • *****
  • Posts: 2174
Re: Readln in lazarus pascal not working
« Reply #9 on: November 05, 2019, 02:49:09 am »
I have to type 1 then enter then either enter or type 2 twice...sigh
What you describe sounds like there is an extra (unwanted) "read" somewhere in your code.

Can you reproduce the problem in a short, standalone program ?  If you could post a short program that exhibits the problem, it would be easier to determine the cause.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 10706
Re: Readln in lazarus pascal not working
« Reply #10 on: November 05, 2019, 07:09:54 am »
complete version:
Code: Pascal  [Select][+][-]
  1. program numbers;
  2. {$ifdef mswindows}{$apptype console}{$endif}
  3. var
  4.  number,number2:integer;
  5. begin
  6.   writeln('enter number');
  7.   readln(number);
  8.   writeln('enter number2');
  9.   readln(number2);
  10.   writeln(number);
  11.   writeln(number2);
  12. end.
Works (of course.....)....
Note Lazarus is -still - not really friendly with console programs and you should never (almost never) use such code in a GUI app if that is your confusion.
« Last Edit: November 05, 2019, 07:17:17 am by Thaddy »

SW48

  • New Member
  • *
  • Posts: 36
Re: Readln in lazarus pascal not working
« Reply #11 on: November 05, 2019, 02:23:37 pm »
Thanks!

When I run your program it works!

Now I have to figure out why mine doesn't.

Maybe its the top line:  {$ifdef mswindows}{$apptype console}{$endif}

SW48

  • New Member
  • *
  • Posts: 36
Re: Readln in lazarus pascal not working
« Reply #12 on: November 05, 2019, 02:33:07 pm »
complete version:
Code: Pascal  [Select][+][-]
  1. program numbers;
  2. {$ifdef mswindows}{$apptype console}{$endif}
  3. var
  4.  number,number2:integer;
  5. begin
  6.   writeln('enter number');
  7.   readln(number);
  8.   writeln('enter number2');
  9.   readln(number2);
  10.   writeln(number);
  11.   writeln(number2);
  12. end.
Works (of course.....)....
Note Lazarus is -still - not really friendly with console programs and you should never (almost never) use such code in a GUI app if that is your confusion.

Is anyone able to look at my code and find the error for a small paypal donation?

lainz

  • Hero Member
  • *****
  • Posts: 3964
  • Leandro Diaz
Re: Readln in lazarus pascal not working
« Reply #13 on: November 05, 2019, 02:53:58 pm »
Yes debugging is hard, but is something you must learn to do. Try adding extra writeln in your code to see where it fails.
Maybe you're using just write instead of writeln?
https://lainz.github.io/ - My Website :)
https://lazpaint.github.io/ -  Download LazPaint

SW48

  • New Member
  • *
  • Posts: 36
Re: Readln in lazarus pascal not working
« Reply #14 on: November 05, 2019, 03:20:31 pm »
When I debug and step into it line by line it works fine.

When I take the debug step by step out, it does not work and I have to type the number twice.

I've used pascal since 1988 and have never seen anything like this.

 

TinyPortal © 2005-2018