Ada: entering user input into a string (1..10) and filling in the rest with spaces

I have identified

subtype String10 is String(1..10); 

and I'm trying to get keyboard input without having to manually enter spaces before pressing the enter button. I tried get_line (), but for some reason, it actually didn’t wait for the input to get get put () output, and I also think that it will just leave everything on the line and not fill the space.

I know and used Bounded_String and Unbounded_String, but I'm wondering if there is a way to make this work.

I tried to make a function for it:

 --getString10-- procedure getString10(s : string10) is c : character; k : integer; begin for i in integer range 1..10 loop get(c); if Ada.Text_IO.End_Of_Line = false then s(i) := c; else k := i; exit; end if; end loop; for i in integer range k..10 loop s(i) := ' '; end loop; end getString10; 

but here I know that s(i) not working, and I do not think that

 "if Ada.Text_IO.End_Of_Line = false then" 

does what I hope he does. This is kind of just a placeholder while I'm looking for the actual way to do this.

I searched for a couple of hours, but the Ada documentation is not as accessible or transparent as other languages. I learned a lot about getting strings, but not what I'm looking for.

+6
source share
2 answers

Just pre-initialize the string with spaces before calling Get_Line .

Here is a small program that I just threw together:

 with Ada.Text_IO; use Ada.Text_IO; procedure Foo is S: String(1 .. 10) := (others => ' '); Last: Integer; begin Put("Enter S: "); Get_Line(S, Last); Put_Line("S = """ & S & """"); Put_Line("Last = " & Integer'Image(Last)); end Foo; 

and the output that I get at startup:

 Enter S: hello S = "hello " Last = 5 

Another possibility, rather than pre-initializing the string, is to set the remainder to spaces after calling Get_Line :

 with Ada.Text_IO; use Ada.Text_IO; procedure Foo is S: String(1 .. 10); Last: Integer; begin Put("Enter S: "); Get_Line(S, Last); S(Last+1 .. S'Last) := (others => ' '); Put_Line("S = """ & S & """"); Put_Line("Last = " & Integer'Image(Last)); end Foo; 

For very large arrays, the latter approach may be more efficient, since it does not assign the initial part of the string twice, but in practice the difference is unlikely to be significant.

+6
source

Alternatively, use the function Get_Line , which returns a fixed String , which has a lower bound of 1 and an upper bound to the number of characters read. "The Line_By_Line example uses an option that is read from a file. If necessary, you can use the procedure Move to copy the Source line to the Target line; the procedure automatically fills the default space.

Appendix: For example, these are Line_Test gaskets with * and silently truncate the long lines to the right.

 with Ada.Integer_Text_IO; with Ada.Strings.Fixed; with Ada.Text_IO; procedure Line_Test is Line_Count : Natural := 0; Buffer: String(1 .. 10); begin while not Ada.Text_IO.End_Of_File loop declare Line : String := Ada.Text_IO.Get_Line; begin Line_Count := Line_Count + 1; Ada.Integer_Text_IO.Put(Line_Count, 0); Ada.Text_IO.Put_Line(": " & Line); Ada.Strings.Fixed.Move( Source => Line, Target => Buffer, Drop => Ada.Strings.Right, Justify => Ada.Strings.Left, Pad => '*'); Ada.Integer_Text_IO.Put(Line_Count, 0); Ada.Text_IO.Put_Line(": " & Buffer); end; end loop; end Line_Test; 
+2
source

All Articles