Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Ada:让用户输入一个字符串(1..10),并用空格填充其余部分_String_Input_Ada - Fatal编程技术网

String Ada:让用户输入一个字符串(1..10),并用空格填充其余部分

String Ada:让用户输入一个字符串(1..10),并用空格填充其余部分,string,input,ada,String,Input,Ada,我已经定义了 subtype String10 is String(1..10); 我正试图通过键盘输入,而不必在按enter键之前手动输入空格。我尝试了get_line(),但出于某种原因,它实际上不会在输出get put()命令之前等待输入,而且我还认为它只会保留前面字符串中的内容,而不会用空格填充 我知道并使用过有界字符串和无界字符串,但我想知道是否有办法做到这一点 我试着为它创建一个函数: --getString10-- procedure getString10(s : string

我已经定义了

subtype String10 is String(1..10);
我正试图通过键盘输入,而不必在按enter键之前手动输入空格。我尝试了get_line(),但出于某种原因,它实际上不会在输出get put()命令之前等待输入,而且我还认为它只会保留前面字符串中的内容,而不会用空格填充

我知道并使用过有界字符串和无界字符串,但我想知道是否有办法做到这一点

我试着为它创建一个函数:

--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;
但是,在这里,我知道
s(I)
不起作用,我认为

"if Ada.Text_IO.End_Of_Line = false then" 
做我希望它也能做的事。这只是一个占位符,而我在寻找实际的方法


我已经搜索了几个小时了,但是Ada文档并不像其他语言那样可用或清晰。我发现了很多关于获取字符串的方法,但不是我想要的。或者,使用其中一种方法,它返回一个固定长度的字符串,该字符串“下限为1,上限为读取的字符数”。示例使用了从文件读取的变体。如果需要,您可以使用将
字符串复制到
目标
字符串;默认情况下,该过程会自动填充空间

附录:例如,此
行\u测试
使用
*
填充,并无声地截断右侧的长行

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;

只需在调用
Get\u Line
之前用空格预先初始化字符串

这是我刚刚拼凑的一个小程序:

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;
以及运行时得到的输出:

Enter S: hello
S = "hello     "
Last =  5
另一种可能,而不是预先初始化字符串,是将余数设置为
Get\u行
调用后的空格:

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;

对于非常大的数组,后一种方法可能更有效,因为它不会两次分配字符串的初始部分,但实际上差异不大。

我尝试了get_line(),但出于某种原因,它在等待输入之前一直打印以下put()。我实际上是从键盘获得输入,但有时也使用输入文件。我来看看是否能用Move()使它工作。谢谢。您可能看到了描述的效果。如果你用新代码更新你的问题,请点击这里。与
true
false
的相等性比较不是特别清楚。如果Ada.Text\U IO.End\U Of_Line=false,则写
,而不是
,如果不是Ada.Text\U IO.End\U Of_Line,则写
。(尽管我认为您不需要使用
End\u Of_Line
)谢谢。我不知道为什么我没有想到只是预先初始化它。“(others=>”)是否只是用空格填充S?到目前为止,我只在case结构中使用过它,所以我不确定它还能做什么。+1用于实际解决问题。:-)@user1279914:另请参见。是的,数组聚合中的
others
子句引用所有未另行指定的元素。如果只有
others
子句,则它引用数组的所有元素。