0

I am reading from standard input (a text file and doing calculations with the data which is lined up like this:

 2 --This states the amount of following sets of info
 150 -- this is the first set of data
 250 -- this is the second set of data
 0 -- this is supposed to tell my program that this is the end of the two sets but
      keep looping because there might be multiple sets in here, seperated by "0"'s. 

Basic outline of my ADA program:

procedure test is

begin 

  while not end_of_file loop
  ......//my whole program executes


  end loop;
end test; 

I want to know how to tell my program to keep looping until theres nothing to read but to keep in mind that the ZERO's seperate the data sets and to keep looping if there is more data after each "0".

user2855405
  • 495
  • 2
  • 7
  • 20

2 Answers2

3

I think this program would meet your requirement, with no need to exit loops prematurely:

with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
with Ada.Text_Io; use Ada.Text_Io;

procedure Reading_Data is
begin
   while not End_Of_File loop
      declare
         Number_Of_Sets : Natural;
      begin
         Get (Number_Of_Sets);
         if Number_Of_Sets > 0 then
            declare
               Sum : Integer := 0;
            begin
               for J in 1 .. Number_Of_Sets loop
                  declare
                     Tmp : Integer;
                  begin
                     Get (Tmp);
                     Sum := Sum + Tmp;
                  end;
               end loop;
               Put ("sum of");
               Put (Number_Of_Sets);
               Put (" elements is ");
               Put (Sum);
               New_Line;
            end;
         end if;
      end;
   end loop;
end Reading_Data;

However, it doesn’t need the 0 separator between sets; 0 just means “this is a set with no elements, ignore it”.

Now, if this a reduced example from a problem where you need to check data consistency (i.e. if you promised 2 elements, then after reading the 2 elements you are either at end of file or there’s a 0) this solution wouldn’t be right. (And you might think I’ve gone overboard with the declare blocks to minimise the scope of the variables ...)

With input:

1
10
0
2
20 30
3 40 50 60

the program gives output:

sum of          1 elements is          10
sum of          2 elements is          50
sum of          3 elements is         150
Simon Wright
  • 25,108
  • 2
  • 35
  • 62
1

Write your loop using labels:

Until_Loop :
   While not end_of_file loop

      X := X + 1;
      ......//my whole program executes;

      exit Until_Loop when X > 5;//change criteria to something 
                                 //relating to no more files 
                                 //(whatever that will be)
   end loop Until_Loop;  

EDIT - question about nested loop in comments:

Example: from here

Named_Loop:
   for Height in TWO..FOUR loop
      for Width in THREE..5 loop
         if Height * Width = 12 then
            exit Named_Loop;
         end if;
         Put("Now we are in the nested loop and area is");
         Put(Height*Width, 5);
         New_Line;
      end loop;
   end loop Named_Loop;
egilhh
  • 6,464
  • 1
  • 18
  • 19
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • I forgot about until - loops. But the problem is the criteria for my program to stop running is just when it has reached the end of the file. But right now I give it more than one set of data and it only executes the first one and ignores the second. I want to somehow tell it that a "0" between sets indicates KEEP GOING. – user2855405 Feb 12 '15 at 22:29
  • `Until_Loop` is just a label, similar to what a `goto` statement uses in C. The `goto` here is implicit. The `exit` statement becomes the only way to leave. Provide the part of your code that shows how it evaluates & branches on the `0`, then maybe I can suggest something. – ryyker Feb 12 '15 at 22:32
  • Since I already have an un-related until loop in my body of code, its giving me a compile error saying that the two until loops are messing with each other. Can you not nest until loops in ADA? – user2855405 Feb 12 '15 at 22:40
  • Yes you can, use unique named labels (again). See edit for example. – ryyker Feb 12 '15 at 22:44
  • Okay they worked out, but I still don't know what clause to put in my until_loop in the "exit when:" for my program to know that there is another set if there is a "0" at the end of a data-set. – user2855405 Feb 12 '15 at 22:54
  • As I suggested above, in a previous comment, if you provide the way you are evaluating and branching on `0`, then perhaps I will understand enough to offer a suggestion. I have to leave soon. apologies. ***[Perhaps this example](https://books.google.com/books?id=X_VlpfGoQRgC&pg=PA213&lpg=PA213&dq=ADA+branching+on+a+0+end+of+data+set&source=bl&ots=H7klbdNeUz&sig=OI9Fc7eus4810A_mb1WkuLcs58s&hl=en&sa=X&ei=WzDdVP-FJ470oATj34CADA&ved=0CB4Q6AEwAA#v=onepage&q=ADA%20branching%20on%20a%200%20end%20of%20data%20set&f=false)*** will give you some ideas... (they refer to using a series of data sets) – ryyker Feb 12 '15 at 23:06
  • From the linked web site: `TWO : constant INTEGER := 2;`. Good grief. – Simon Wright Feb 13 '15 at 12:10
  • @SimonWright What's especially mystifying is that this is the kind of code I expect from someone who has learned some _rule_ without learning why we have the rule, so they think they have to use named constants for everything--and then they write code like `for Index in TWO..THREE**2 - FOUR`. Why did they use the literal 2 in there?? – ajb Feb 14 '15 at 05:14