adaada95

Ada 95 Check if enter was pressed


The while loop should be able to check whether the input character is a white space or an enter key.

Furthermore, how can I check the user input type, like the type() or typeof() function in other languages?

FUNCTION ReadValue RETURN Unbounded_String IS
      ValueChar : Character;
      Result : Unbounded_String := To_Unbounded_String("NULL");
   BEGIN
      Get(ValueChar);
      Skip_Line;
      
      WHILE ValueChar /= ';'LOOP
         Get(ValueChar);
         IF IsValidNameInput(ValueChar) THEN 
            Result := Result & ValueChar;
         ELSE
            exit;
         END IF;         
      END LOOP;
      ValueIntegerFlag  := CheckValue(Value);
      
      RETURN Result;
      
   END ReadValue; 

Solution

  • Read the characters one-at-a-time without special ENTER handling using Get_Immediate instead of Get - ARM A.10.7(9).

    You can do checks on the class of the character you’ve just read using Ada.Characters.Handling - ARM A.3.2 - something like

    function Is_Valid_Name_Input (Ch : Character) return Boolean is
    begin
       return Ada.Characters.Handling.Is_Graphic (Ch)
         and then not Ada.Characters.Handling.Is_Space (Ch);
    end Is_Valid_Name_Input;
    

    (probably not quite what you want, since it makes &*^$$^ a valid name!)

    Ada.Characters.Handling.Is_Line_Terminator detects ENTER (on Unix; probably on Windows too).

    You can check whether a string corresponds to an integer by trying the conversion and catching the exception when it fails:

    function Check_Integer_Value (Str : Unbounded_String) return Boolean is
       Dummy : Integer;
    begin
       Dummy := Integer'Value (To_String (Str));
       return True;
    exception
       when Constraint_Error =>
          return False;
    end Check_Integer_Value;
    

    With regard to ReadValue:

    Try something like

    function Read_Value return Unbounded_String is
       Value_Char : Character;
       Result : Unbounded_String;
    begin
       loop
          Get_Immediate (Value_Char);
          exit when Value_Char = ';';
          if Is_Valid_Name_Input (Value_Char) then
             Result := Result & Value_Char;
          end if;
       end loop;
       return Result;
    end Read_Value;