Module 2 – Use types to describe valid data
Give values a meaningful range
A type describes values and permitted operations. A subtype can narrow a type: a score in this exercise is an integer from zero through one hundred. Raw is deliberately a wider Integer so that we can represent input which is not yet trusted. The membership test asks whether that value belongs to Score before converting it.
with Ada.Text_IO;
procedure Main is
subtype Score is Integer range 0 .. 100;
Raw : Integer := 101;
begin
if Raw in Score then
Ada.Text_IO.Put_Line ("Accepted" & Score'Image (Score (Raw)));
else
Ada.Text_IO.Put_Line ("Score must be between 0 and 100");
end if;
end Main;With Raw = 101, the example prints the range message. Change it to 100 and predict the accepted message. Score'Image produces a string; for nonnegative integer values, the image includes a leading space. Treat formatting and correctness as separate questions.
Reference: AdaCore: types and ranges.
Complete this lesson
Answer every check correctly to earn your lesson certificate. Feedback helps you review and try again.
Sign in · Save your progress and earn a certificate for this lesson.