Ada:让用户输入到String(1..10),然后用空格填充其余部分 [英] Ada: Getting user input to a String(1..10) and filling the rest with whitespace

查看:95
本文介绍了Ada:让用户输入到String(1..10),然后用空格填充其余部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已定义

 子类型String10是String(1..10); 

,而我试图获得键盘输入,而不必在按Enter之前手动输入空格。我尝试了get_line(),但是由于某种原因,它实际上不会等待输入才输出get put()命令,而且我也认为它只会在字符串之前留下任何内容,而不会用空格填充。 / p>

我了解并使用了Bounded_String和Unbounded_String,但我想知道是否有办法使它工作。



我尝试为其创建函数:

 -getString10-- 
过程getString10(s :string10)是
c:字符;
k:整数;
在整数范围1..10中为i开始
循环
get(c);
如果Ada.Text_IO.End_Of_Line = false,则
s(i):= c;
else
k:= i;
出口;
结尾,如果;
结束循环;

for i in in integer range k..10 loop
s(i):=’’;
结束循环;
end getString10;

但在这里,我知道 s(i)不起作用,我不认为

 如果Ada.Text_IO.End_Of_Line = false,那么 

做我希望它能做的事。



我一直在搜索几个小时,但Ada文档尚不可用或不清楚和其他语言一样。我发现了很多有关获取字符串的信息,但不是我要找的东西。

解决方案

只需预初始化字符串在调用 Get_Line 之前使用空格。



这里是一个我刚刚放在一起的小程序:

 和Ada.Text_IO;使用Ada.Text_IO; 
过程Foo是
S:String(1 .. 10):=(others =>’’);
最后:整数;
开始
Put( Enter S:);
Get_Line(S,Last);
Put_Line( S =& S&);
Put_Line( Last =& Integer’Image(Last));
结束Foo;

以及运行它时得到的输出:

 输入S:hello 
S = hello
Last = 5

另一种可能性是,将字符串的其余部分设置为 之后的空格,而不是预先初始化字符串。 $ c>呼叫:

 和Ada.Text_IO;使用Ada.Text_IO; 
过程Foo是
S:String(1 .. 10);
最后:整数;
开始
Put( Enter S:);
Get_Line(S,Last);
S(Last + 1 .. S’Last):=(others =>’’);
Put_Line( S =& S&);
Put_Line( Last =& Integer’Image(Last));
结束Foo;

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


I have defined

subtype String10 is String(1..10);

and I am attempting to get keyboard input to it without having to manually enter whitespace before hitting enter. I tried get_line() but from some reason it wouldn't actually wait for input before outputting the get put() command, and I also think it will just leave whatever was in the string before there and not fill it with white space.

I know about and have used Bounded_String and Unbounded_String, but I am wondering if there is a way to make this work.

I've tried making 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 the s(i) doesn't work, and I don't think the

"if Ada.Text_IO.End_Of_Line = false then" 

does what I'm hoping it will do either. It's kinda just a placeholder while I look for the actual way to do it.

I been searching for a couple hours now, but Ada documentation isn't as available or clear as other languages. I've found a lot about getting strings, but not what I'm looking for.

解决方案

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

Here's a little program 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 I get when I run it:

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

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

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 might be more efficient because it doesn't assign the initial portion of the string twice, but in practice the difference is unlikely to be significant.

这篇关于Ada:让用户输入到String(1..10),然后用空格填充其余部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆