Compare a string input to an Enumerated type

前端 未结 3 1116
北恋
北恋 2021-01-07 12:18

I am looking to do a comparison of a string to and enumeration. I have written a sample code of what I am attempting. Since a String and and Enumerated type are different, h

3条回答
  •  遥遥无期
    2021-01-07 12:56

    Another possibility:

    Get_Line (response, N);
    declare
        Color : StopLightColor;
    begin
        Color := StopLightColor'Value(response(1..N));
        -- if you get here, the string is a valid color
        Put_Line ("The stoplight is " & response(1..N));
    exception
        when Constraint_Error =>
            -- if you get here, the string is not a valid color (also could
            -- be raised if N is out of range, which it won't be here)
            null;
    end;
    

提交回复
热议问题