9

I just learnt about Enums and Types in Ada and decided to write a small program to practice:

with Ada.Text_IO;                       use Ada.Text_IO;
with Ada.Integer_Text_IO;       use Ada.Integer_Text_IO;

procedure Day is 

    type Day_Of_The_Week is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);

    subtype Weekday is Day_Of_The_Week range Monday..Friday;

    subtype Weekend is Day_Of_The_Week range Saturday..Sunday;

        function is_Weekday ( dayOfTheWeek: in Day_Of_The_Week) return Boolean is
        begin
            if(--?--)
        end is_Weekday;

    selected_day_value  :   Integer;
    selected_day                :   Day_Of_The_Week;

begin
    Put_Line("Enter the number co-responding to the desired day of the week:");
    Put_Line("0 - Monday");
    Put_Line("1 - Tuesday");
    Put_Line("2 - Wednesday");
    Put_Line("3 - Thursday");
    Put_Line("4 - Friday");
    Put_Line("5 - Saturday");
    Put_Line("6 - Sunday");
    Get(selected_day_value);
    selected_day = Day_Of_The_Week'pos(selected_day_value);

    if( is_Weekday(selected_day))
        Put_Line( Day_Of_The_Week'Image(selected_day) & " is a weekday." );
    else
        Put_Line( Day_Of_The_Week'Image(selected_day) & " is a weekday." );

end Day;

I'm having trouble with the if statement. How can I check whether or not dayOfTheWeek is in the Weekday subtype or the weekend subtype?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
W.K.S
  • 9,787
  • 15
  • 75
  • 122
  • Just being a pedant, but your code assumes valid input. IRL you should use a representation clause on your enum to guarantee the range 0 - 6, and get an integer, use a unchecked conversion to your integer type, check it is valid, and then check the weekend/weekday subtypes. – NWS Jan 30 '12 at 09:43
  • Why not input a `Day_Of_The_Week` explicitly? `package Day_Of_The_Week_Text_IO is new Ada.Text_IO.Enumeration_IO (Day_Of_The_Week);` and then `Get (Selected_Day); Skip_Line;` – Jacob Sparre Andersen Oct 12 '13 at 08:30

2 Answers2

9

You want

function is_Weekday ( dayOfTheWeek: in Day_Of_The_Week) return Boolean is
begin
    return dayoFTheWeek in Weekday;
end is_Weekday;

Also, you want ’Val not ’Pos in

selected_day := Day_Of_The_Week'val(selected_day_value);

and you might take a look at the words in the second Put_Line!

Simon Wright
  • 25,108
  • 2
  • 35
  • 62
2

You don't need a function to check for this. In this case a function only obscures what happens:

if Selected_Day in Weekday then
  do stuff..
else
  do other stuff...
end if;
Jacob Sparre Andersen
  • 6,733
  • 17
  • 22
James Adam
  • 2,324
  • 3
  • 16
  • 19