mainstringargs
mainstringargs

Reputation: 14381

Printing Out Characters in Ada

I have these declared:

  subtype Num_Char          is Natural 
     range 1 .. Definitions.Page_Width + 1;
  subtype Number_Of_Rows    is Definitions.Number_Of_Rows;
  type Chars                is array (Number_Of_Rows, Num_Char) of Character; 
  The_Chars           : Chars;

What is the best way to print this out to the screen using Ada.Text_IO.Put_Line()?

Upvotes: 3

Views: 5776

Answers (2)

T.E.D.
T.E.D.

Reputation: 44804

A lot of problems in Ada actually go back to your initial choice of types. So personally, I'd suggest a slight rewrite to make your life easier:

subtype Row is String (1..Definitions.Tote_Page_Width + 1);
type Chars is array (Definitions.Number_Of_Rows) of Row;

Now you could write this out with the following:

for I in The_Chars'range loop
    Ada.Text_IO.Put_Line (The_Chars (I));
end loop;

However, there is still a big problem here. Put_Line will print out all the characters in each row. Ada strings are not null-terminated, so if there unused data at the end of some of your lines, that will get printed too.

There are lots of ways to deal with this, but they are very different techniques than would be used to handle C strings. If you try to handle Ada strings like you'd handle C strings, you will drive yourself nuts.

For this reason, I would dearly like to see your code that actually fills The_Char with data (and the rationale behind it).

Upvotes: 3

Simon Wright
Simon Wright

Reputation: 25501

Assuming you want to use Ada.Text_IO and not just Put_Line specifically, and assuming that Number_Of_Rows is meant to be an integer range like Num_Char, that would be

for R in The_Chars'Range (1) loop
   for C in The_Chars'Range (2) loop
      Ada.Text_IO.Put (The_Chars (R, C));
   end loop;
   Ada.Text_IO.New_Line;
end loop;

Upvotes: 4

Related Questions