I have Ada program that runs and compile perfectly using GNAT - GPS. When I run its exe file and provide user input then instead of saying "Press any key to continue", the exe closes immediately.
I have searched for it online alot but i only found info related to c/c++/visual studio console window using system('pause'); OR Console.Readline().
Is there any way around for this in Ada lanaguage?
Apart from using Get_Line
or Get
, you can also use Get_Immediate
from the Ada.Text_IO
package. The difference is that Get_Line
and Get
will continue to read user input until <Enter>
has been hit, while Get_Immediate
will block only until a single key has been pressed when standard input is connected to an interactive device (e.g. a keyboard).
Here's an example:
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
begin
-- Do some interesting stuff here...
declare
User_Response : Character;
begin
Put_Line ("Press any key to continue...");
Get_Immediate (User_Response);
end;
end Main;
NOTES
You should run the program in an interactive terminal (Bash, PowerShell, etc.) to actually see the effect of Get_Immediate
. When you run the program from within GPS, then you still have to press enter to actually exit the program.
This might be too much detail, but I think that Get
still waits for <Enter>
to be pressed because it uses fgetc
from the C standard library (libc) under the hood (see here and here). The function fgetc
reads from a C stream. C streams are initially line-buffered for interactive devices (source).