Raised ADA.IO_EXCEPTIONS.DATA_ERROR : a-tiinio.adb:104 instantiated at guess_number.adb:5


Here’s another Rosetta Code Task that does not work for me “out of the box”, which I think it should.

Guess the number

Guess the number - Rosetta Code

cat -n ./guess_number.adb
1 with Ada.Numerics.Discrete_Random;
2 with Ada.Text_IO;
3 procedure Guess_Number is
4 subtype Number is Integer range 1 … 10;
5 package Number_IO is new Ada.Text_IO.Integer_IO (Number);
6 package Number_RNG is new Ada.Numerics.Discrete_Random (Number);
7 Generator : Number_RNG.Generator;
8 My_Number : Number;
9 Your_Guess : Number;
10 begin
11 Number_RNG.Reset (Generator);
12 My_Number := Number_RNG.Random (Generator);
13 Ada.Text_IO.Put_Line (“Guess my number!”);
14 loop
15 Ada.Text_IO.Put ("Your guess: ");
16 Number_IO.Get (Your_Guess);
17 exit when Your_Guess = My_Number;
18 Ada.Text_IO.Put_Line (“Wrong, try again!”);
19 end loop;
20 Ada.Text_IO.Put_Line (“Well guessed!”);
21 end Guess_Number;

What is the correct solution to fix the error?


Guess it’s my time for a nap (SIGH)

I just did not see the 1…10 subtype (I do now) and I had entered “12” when executing the program earlier.

I’m sorry for wasting everyone’s time with this one.


1 Like

I would have expected a Constraint_Error if you entered a valid number, it is just out of range.

ARM A.13(13), “The exception Data_Error can be propagated by the procedure Read (or by the Read attribute) if the element read cannot be interpreted as a value of the required subtype.” So it’s reasonable in this case (just as if you’d typed foo).