简体   繁体   English

Ada95在记录中遍历数组并打印字符

[英]Ada95 Loop through Array within a Record and print the characters

The program below is suppose to get an input of Characters from the user and put it into an Record with the subprogram Procedure Get(Tecken : out Record_Type) . 假设下面的程序从用户那里获取了Characters的输入,并使用子程序Procedure Get(Tecken:out Record_Type)将其输入到Record中。 The last step is to Print it in the Terminal with another subprogram Procedure Put(Chars : in Chars_Array) . 最后一步是使用另一个子程序Procedure Put(Chars:in Chars_Array)在终端中打印它。

The issue I'm facing is the actual output of the Characters into the Terminal where I get: 我面临的问题是将字符实际输出到终端的位置:

Picture of the Output 输出图片

procedure Poster is

type Char_Array is                            -- Fält/Array
    array (1..256) of Character;

type Record_Type is                           -- Post/Record
    record
        Chars : Char_Array;
        Length : Integer;
    end record;

procedure Get(Tecken : out Record_Type) is   -- Character input

begin

    for I in 1..256 loop
        Get(Tecken.Chars(I));
        exit when End_Of_Line;
    end loop;

end Get;      

Here is the subprogram that prints the Characters from the Array. 这是从数组打印字符的子程序。 I'm confused when it comes to Records and Arrays, but I'm thinking that I need an Array as a inparameter since I can't loop through Records. 对于记录和数组,我感到困惑,但我认为我需要数组作为参数,因为我无法遍历记录。

procedure Put(Chars : in  Char_Array) is   -- Character output

begin

    for I in 1..256 loop
        Put(Chars(I));
    end loop;

end Put;   

Here is another bump in the road where I use an inparameter "Chars" the Put-Call. 这是我使用参数“ Chars” Put-Call的道路上的另一个障碍。 Does the Char_Array recognize the input in "Tecken" ? Char_Array是否可以识别“ Tecken”中的输入?

Tecken : Record_Type;
Chars : Char_Array; 

begin

    Get(Tecken);
    Put(Chars);

end Poster;

There's a bit of a warning from the compiler: 编译器发出了一些警告:

poster.adb:31:04: warning: variable "Chars" is read but never assigned

This is because Get writes to Tecken.Chars ; 这是因为Get写入Tecken.Chars the Chars you pass to Put has nothing to do with it, and is filled with random data. 您传递给PutChars与它无关,并且充满了随机数据。

You say "I'm thinking that I need an Array as a inparameter since I can't loop through Records". 您说“我正在考虑需要一个数组作为参数,因为我无法遍历记录”。 True, you can't loop through a record, but you can loop through an array that is part of a record. 是的,您不能遍历一条记录,但是可以遍历记录的一部分的数组。

If you say 如果你说

Put(Tecken.Chars);

things work better, though you still get garbage after the actual input. 事情工作得更好,尽管在实际输入之后仍然会产生垃圾。 You might expect even better if you say 如果您说,您可能会期望更好

   Put(Tecken.Chars (1 .. Tecken.Length));

but

$ ./poster 
0123456789

raised CONSTRAINT_ERROR : poster.adb:36 range check failed

which is because Get doesn't actually set Tecken.Length . 这是因为Get实际上没有设置Tecken.Length

After fixing this[*], this should do the trick 修复此[*]之后,就可以解决问题

procedure Put (Tecken : in  Record_Type) is
begin
   for I in 1 .. Tecken.Length loop
      Put (Tecken.Chars(I));
   end loop;
end Put;

Now, the call is just 现在,电话只是

Put (Tecken);

[*] It's not as easy to fix Get as you might hope, since when End_Of_Line is true the character returned is the last one on the line. [*]修复Get并非如您所愿那样容易,因为当End_Of_Line为true时,返回的字符是该行的最后一个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM