adagnat-gps

How to stop console window from closing immediately | GNAT - GPS


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?


Solution

  • 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