Recent

Author Topic: readkey problem in pascal  (Read 7223 times)

7vinbaby

  • Jr. Member
  • **
  • Posts: 59
  • Hi ! Bless you have a good day.
readkey problem in pascal
« on: September 27, 2016, 04:04:22 pm »
here's my code


procedure example1;
........
procedure example2;
........
begin
writeln('Press Enter to continue/Press ESC to exit');
ch:=ReadKey;
case ch of
#13:begin
       example1;
       example2;
       end;
#27:break;
end.

i want the user press ESC to end the program while the user press Enter to continue.
can it be worked?
7vinbaby

7vinbaby

  • Jr. Member
  • **
  • Posts: 59
  • Hi ! Bless you have a good day.
Re: readkey problem in pascal
« Reply #1 on: September 27, 2016, 04:09:05 pm »
here's my code


procedure example1;
........
procedure example2;
........
begin
writeln('Press Enter to continue/Press ESC to exit');
ch:=ReadKey;
case ch of
#13:begin
       example1;
       example2;
       end;
#27:break;
end.

i want the user press ESC to end the program while the user press Enter to continue.
can it be worked?

i test the program but i find i can press enter to continue .However it cannot stop while i press ESC
7vinbaby

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: readkey problem in pascal
« Reply #2 on: September 27, 2016, 04:18:30 pm »
It depends on what of kind of the 'stop' you want.

Break does not end the program, it just exits the current looping.
http://www.freepascal.org/docs-html/rtl/system/break.html

Have you try to use Exit or Halt?
http://www.freepascal.org/docs-html/rtl/system/exit.html
http://lazarus-ccr.sourceforge.net/docs/rtl/system/halt.html

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: readkey problem in pascal
« Reply #3 on: September 27, 2016, 04:20:47 pm »
Handoko is correct there. The break makes no sense whatsoever there.

Code: Pascal  [Select][+][-]
  1. program Test;
  2.  
  3. uses
  4.   crt;
  5.  
  6. procedure example1;
  7. begin
  8.   WriteLn('example 1');
  9. end;
  10.  
  11. procedure example2;
  12. begin
  13.   WriteLn('example 2');
  14. end;
  15.  
  16. var
  17.   ch : Char;
  18. begin
  19.   writeln('Press Enter to continue/Press ESC to exit');
  20.   ch := ReadKey;
  21.   case ch of
  22.     #13:
  23.     begin
  24.       example1;
  25.       example2;
  26.     end;
  27.     #27: Exit;
  28.   end;
  29.   WriteLn('graceful program exit');
  30. end.
  31.  
« Last Edit: September 27, 2016, 04:26:13 pm by molly »

7vinbaby

  • Jr. Member
  • **
  • Posts: 59
  • Hi ! Bless you have a good day.
Re: readkey problem in pascal
« Reply #4 on: September 27, 2016, 04:21:31 pm »
It depends on what of kind of the 'stop' you want.

Break does not end the program, it just exits the current looping.
http://www.freepascal.org/docs-html/rtl/system/break.html

Have you try to use Exit or Halt?
http://www.freepascal.org/docs-html/rtl/system/exit.html
http://lazarus-ccr.sourceforge.net/docs/rtl/system/halt.html

i use exit and it works.Thx for your helps!!
7vinbaby

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: readkey problem in pascal
« Reply #5 on: September 27, 2016, 04:27:08 pm »
Handoku is correct there. The break makes no sense whatsoever there.
Yes, but since the exit is in main, halt or halt(0); I would prefer  Halt. It is his intention to stop the program.
Specialize a type, not a var.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: readkey problem in pascal
« Reply #6 on: September 27, 2016, 04:33:34 pm »
These are similar but different, often confuse novice programmers:
- Break
- Exit
- Halt
- Halt(errnum)
- RunError(errorcode)
- TApplication.Terminate
- TForm.Close

It would be glad if someone write a wiki page describing their differences. I can't because my English is not good.
« Last Edit: September 28, 2016, 09:15:53 pm by Handoko »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: readkey problem in pascal
« Reply #7 on: September 27, 2016, 04:38:51 pm »
Handoku is correct there. The break makes no sense whatsoever there.
Yes, but since the exit is in main, halt or halt(0); I would prefer  Halt. It is his intention to stop the program.
Fair enough for the case of TS.

However, there is a drawback using halt in case you have initialized all kinds of stuff before invoking the halt. That would require an AddExitProc to neatly free up allocated resources. That is why i prefer exit strategy in certain situations (as the AddExitProc is a disgrace to the language imho).

@handoko:
Don't forget runerror
« Last Edit: September 27, 2016, 04:47:55 pm by molly »

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: readkey problem in pascal
« Reply #8 on: September 28, 2016, 09:14:41 pm »
@handoko:
Don't forget runerror

RunError? It is something new to me. Thanks for the info. I've just checked the documentation of both RunError and Halt(errnum), but not really understand the differences. Can you please explain? Me and many others would like to know.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: readkey problem in pascal
« Reply #9 on: September 28, 2016, 11:24:47 pm »
Well, i am reading the same documentation as you  :)

Looking at the implementation it seems that the difference between runerror and halt is about stacktrace. In the end Runerror calls halt.
Code: [Select]
procedure RunError(w : word);[alias: 'FPC_RUNERROR'];
var
  bp : pointer;
  pcaddr : codepointer;
begin
  errorcode:=w;
  pcaddr:=get_pc_addr;
  bp:=get_frame;
  get_caller_stackinfo(bp,pcaddr);
  erroraddr:=pcaddr;
  errorbase:=bp;
  Halt(errorcode);
end;

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: readkey problem in pascal
« Reply #10 on: September 29, 2016, 10:57:46 am »
These are similar but different, often confuse novice programmers:
- Break

Jump out of current loop.

Quote
- Exit

Jump out of current procedure (in macpas mode there is some extension that allows exiting multiple nested procedures iirc)

If in mainprogram, roughly the same as exit.

Quote
- Halt

Graceful exit (+/- jump to end of mainprogram)

Quote
- Halt(errnum)

Same but sets OS returncode to "errnum".

Quote
- RunError(errorcode)

Raises a runtime error and then exits. Since the runtime error number is returned in the OS returncode it is, as Molly's code shows, equal to halt(x)+backtrace.

Quote
- TApplication.Terminate

Shuts down an Application object, which internally calls halt after (typically GUI) app shuts down. The errorcode can be set in the application object iirc.

Quote
- TForm.Close

Closes the current form. If the current form is the main (first created) form, then it is equal to application.terminate.

 

TinyPortal © 2005-2018