Ada input from a terminal

What’s the reason for the double if/when?

What’s the reason for this nonsense? Avail is out parameter!
Read the RM A.10.7(11): This version of Get_Immediate does not wait for input; if Avail=True, In_Char (an out parameter as well) has been read, if not, Char is undefined.

Did not further explore the incorrect code.

Sorry I may have not a not correct example but my problem what ever I do a line terminator Get_Immesiate as well as Get_Line and Get has to find a line terminator that can be used to stop reading the keyboard in definitly.
So what does the keyboard with , it is not CR or LF

Have you ever tried to read the RM?
The basics:
Text_IO is implementation and hardware agnostic. There are systems where there is no end of line character (like CR/LF), so the operations in Text_IO cannot read an end of line character, even if it exists.
Sorry, you have to live with the operations provided.
For end of line there is function End_of_Line.

So get an Ada book or read all chapters of RM A.10, these parts of the RM are quite understandable.
And stop posting wrong code - please!

I did run a program using the function Read_str as below

function Read_str return String is
In_String : string (1 .. nr_char);
Last : natural;
begin
Put_Line (“Start Read_str”);
Get_Line(In_String, Last);
Put_Line("String = " & In_String(1.. Last));
return In_String(1 .. Last);
end Read_str;

Using Gnat Studio with: Build \ Project \ Build & Run \ Build & Run Program.adb with result from wthin the function

Start Read_str
123str – input

No result. After this I had to stop the prgram wit Task Management

Using Build \ Run I did not get any results

In the Administrator:Powershell I get:

Start Read_str
123str – input
String = 123str

raised CONSTRAINT_ERROR : bad input for 'Value: “123str”
The program could not be seen running in the task managment

The difference could give some answer

The failure message is due to a failure further in the program where 123str should be converted to integer. But it shows that in Powershell the routine is working correct when run in the powershell and makes an error in GNAT Studio 26.0w

It’s again the same mistake you make: You don’t give the complete program! What is the value of nr_char and what do you do with the return value of Read_str?
Waning patience on my side.

I think I have also said so. I do have a package in all my programs called general. In that package all variables, constants, functions and procedures that are used in several packages anywhere in the program are defined in that package. The reason, I only have to change if necessary in one place that is simple traceable. so nr_char is defined in that packages. The function read_str is used as the the base routine in an other standard package I call “Reading” and should be able to use it in various programs as a standard I am creating at least for myself. At I have also made standard packages for using stacks and pipes for use with various types. I now do have a program just enough for testing. So I don’’ see it is useful to find failures in the code I send. If you would find it useful I can sent the package. At this moment that is simple enough with only two other functions, Read_Unb for Unbouded_String’s and Read_int for integers. Those are only used to convert the output of Read_str. At this moment I have not tried using overload.

In the mean time I tried overloading, but the compiler indicates the in my case overloading cannot be used.The problem must be in the Get_Line because the string one statement before is a put_line which is showing and the statement just after is also a put_line which is not showing. The loop stops at that moment. To me it looks like all version of Get do not recognize the .
Again the routine:
function Read_str return String is
In_String : string (1 .. nr_char);
Last : natural;
begin
Put_Line (“Start Read string”); --This Put_Line is presented
Get_Line(In_String, Last);
Put_Line("String = " & In_String(1.. Last)); – This Put_Line is not presented
return In_String(1 .. Last);
end Read_str;

Are you sure using the proper subroutine there?

Your function doesn’t seem to have any parameters, just returning a string. If you are only interested in the return values, shouldn’t you use procedure subroutine with an out parameter?

I am only interested in returning a string that is read from a keyboard that is correct. That string I can easily convert to other type link Unbounded strings, integers etc. I just want a package that makes reading from a keyboard simple like the read fictions in other languages for instance Pascal

When only interested in “returning a string that is read from the keyboard” why not use something like:

   declare
      Input : String := Ada.Text_Io.Get_Line;
   begin
      -- here code for whatever you want to do with Input;
   end;

Question: in your code, what is the value of Nr_Char ? Is it properly initialized in that global package of yours ?
Question : wrt your global package, ever heard of David Parnas and his ideas (in 1972!) of information hiding and system modularization? Maybe that is something to consider

As can be seen in the complete discussion here is: How can Get_Line see the End of Line terminator On a key board you use the key Windows. The program stops because Get_Line is waiting for that terminator. I also tried Ge tand Get_Immediate.
nr_char is a constant declared in Reading.ads.The procedure used where it is used in is Reading.adb For me the question: What is the difference between

declare
Input : String := Ada.Text_Io.Get_Line;
begin
– here code for whatever you want to do with Input;
end;

and:

declare
Input : String(1..nrchar);
begin
Input := Get_Line;
– code
end;

nr_char is maximum character the number of characters for String. The declaration is
nr_char : constant integer := 100; I need to see later what the needed value for that that parameter is.
No I never heard of David Parnas in 1972 I made programs in “Basic” or “Algol 60”. Is there any publication I should see of him regarding my problem. I

First off, please format code when posting, for blocks use three back-ticks (```) to bracket the block (on their own line); for a inline, use single backticks to bracket the item.
Secondly, the difference is obvious once you understand unconstrained arrays, the simple way to explain it being “X :String;” is rejected by the compiler because it doesn’t know the length, while “X : String := "Hello";” takes the length from the initialization to specify the length, and similarly “X : String:= Get_Line;” uses the function’s result to perfectly size the buffer (String) to the value it receives. (See Memory Management with Ada 2012. linked below.)

You can even combine having a buffer with returning long strings via recursively calling a buffered function; as shown in this Adapower.com example:

function Next_Line(File : in Ada.Text_IO.File_Type :=
   Ada.Text_Io.Standard_Input) return String is
   Answer : String(1..256);
   Last   : Natural;
begin
   Ada.Text_IO.Get_Line(File => File,
      Item => Answer,
      Last => Last);
   if Last = Answer'Last then
      return Answer & Next_Line(File);
   else
      return Answer(1..Last);
   end if;
end Next_Line;

PS — J.P. Rosen’s excellent presentation: Memory Management with Ada 2012.

   function Read_str return String is
      In_String : string := Get_Line;
      Last      : natural;
   begin
      Put_Line ("Start Read string");
--      Get_Line(In_String, Last);
      Last := Length(To_Unbounded_String(In_String));
      Put_Line ("Last = " & Natural'Image (Last));
      if Last = 0
      then
         return "0";
      end if;
      return In_String(1 .. Last);
   end Read_str;

The construction with Get_Line in the declaration was one I never used. I use the book software engineering with ada second edition of Grady Booch. I know that may that is for Ada 95. As I need also ‘Last’. But the result I get now is:

Start Read integer
abcds                   -- input

Again no reaction trom Get_Line just as the version with the “In_String : String;” version.and there I can get “Last” directly. So what is the problem.

That can’t be the result of that program. There is no way that

Put_Line ("Start Read string");

gives you this output

Start Read integer

This is what I always have complained about. But he still refuses to show the whole program. He must be doing some really weird things in the caller.

What’s wrong with In_String’Length?

Well, in this case it could be that the caller of Read_Str outputs the given message. In the declarative part of Read_Str there is a call to Get_Line, so the program waits there for input before it continues.
@ldries46 , please, please provide a simple reproducer program so that we don’t have to guess what is going on outside the code you present here.

Idries46:
Please run this program and try to understand what happens here.
I really do hope this gives you insight into how procedure Get_Line works.
Perhaps this insight will help you understanding how function Get_Line works.

with Ada.Text_IO;
use  Ada.Text_IO;

procedure Explore_Get_Line is
  Line: String (1 .. 10);  -- can hold 10 characters
  Last: Natural;
begin
  loop
    Get_Line (Line, Last);                    -- i
    Put_Line ('"' & Line (1 .. Last) & '"');  -- ii
    Put_Line (End_Of_Line'Image);             -- iii
  end loop;
end Explore_Get_Line;

This is what you will see on your screen.
Use lines with Enter at the end as your input.
The other lines are output of the program.
The sequence numbers at the end are used for commenting what
happens in each step.
The roman numbers on the program are used as reference.

> Explore_Get_Line   1
1234567890<Enter>    2
"1234567890"         3
TRUE                 4
""                   5
123<Enter>           6
FALSE                7
"123"                8
12345678901<Enter>
FALSE
"1234567890"
FALSE
"1"

This is the commented output.

1   Start program, it waits at Get_Line i.
    Nothing in the input stream.
2   Input of 10 characters, press Enter key:
    Input stream: 1234567890<EoL>
    i is executed, exactly 10 characters are read,
    0 characters are left to read,
    the line terminator is next.
    Input stream: <EoL>
3   ii is executed, 10 characters are output.
    Input stream: <EoL>
4   iii is executed: End_Of_Line returns True.
    The line terminator is not yet consumed.
    Input stream: <EoL>
5   i is executed, 0 characters are read,
    EoL is consumed.
    ii is executed, 0 characters are ouput.
    Input stream: empty
    Program waits at End_Of_Line.
6   Input of 3 characters, press Enter key:
    Input stream: 123<EoL>
7   iii is executed: End_Of_Line returns False.
8   i is executed, 3 characters are read and output.
    <EoL> is consumed.
    Input stream: empty again.
    End_Of_Line waits again for input.