Ada-提出了Constraint_error:“值: [英] Ada- raised Constraint_error : bad input for 'Value:

查看:181
本文介绍了Ada-提出了Constraint_error:“值:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Integer’Value将字符串转换为Integer。这对于文件的第一个循环工作正常,但是之后我得到了一个错误的'value输入(提高了Constraint_Error。我希望有人可以向我展示我的方式的错误,以便可以将字符串转换为整数)。

 与Ada.Text_IO,Ada.Integer_Text_IO; 
USE Ada.Text_IO,Ada.Integer_Text_IO;

过程Isbntest是

功能Strip(The_String:String; The_Characters:String)
返回字符串IS
保留:ARRAY(字符)OF布尔值:= (OTHERS => True);
结果:String(The_String'RANGE);
Last:Natural:=结果'First-1;
开始
FOR I IN The_Characters'范围LOOP
Keep(The_Characters(I)):= False;
END LOOP;
FOR J IN The_String'RANGE LOOP
IF Keep(The_String(J))THEN
Last:= Last + 1;
Result(Last):= The_String(J);
END IF;
END LOOP;
RETURN Result(Result'Fi rst .. Last);
END St​​rip;


输入:File_Type:= Ada.Text_IO.Standard_Input;

开始

而不是End_of_File(Input)LOOP
DECLARE
Line:String:= Ada.Text_IO.Get_Line(Input);
StrippedLine:字符串:=行;
ascii_val:整数:= 0;

开始
StrippedLine:= Strip(Line,-);
ascii_val:=整数值(StrippedLine);
Put(ascii_val);
Put_line(StrippedLine);
END;
END LOOP;
关闭(输入);
结束测试;


解决方案

问题是您弄乱了长度创建数组之后。不要这样做。



而不是

 声明
行:字符串:= Ada.Text_IO.Get_Line(输入);
StrippedLine:字符串:=行;
开始
StrippedLine:= Strip(Line,-);

只需在声明时将Stripped_Line初始化为正确的大小即可。

 声明
Line:String:= Ada.Text_IO.Get_Line(Input);
StrippedLine:字符串:= Strip(Line,-);
开始

我假设您的 Strip功能在这里正常工作。 p>

I'm trying to use Integer'Value to convert a string to an Integer. This works fine for the first loop through a file, but after that I get a bad input for 'value (raised Constraint_Error. I was hoping someone could show me the error of my ways, so that I can convert the string to an integer on each loop.

WITH Ada.Text_IO, Ada.Integer_Text_IO;
USE Ada.Text_IO, Ada.Integer_Text_IO;

PROCEDURE Isbntest IS

  FUNCTION Strip(The_String: String; The_Characters: String)
        RETURN String IS
     Keep: ARRAY (Character) OF Boolean := (OTHERS => True);
     Result: String(The_String'RANGE);
     Last: Natural := Result'First-1;
  BEGIN
     FOR I IN The_Characters'Range LOOP
        Keep(The_Characters(I)) := False;
     END LOOP;
     FOR J IN The_String'RANGE LOOP
        IF Keep(The_String(J)) THEN
           Last := Last+1;
           Result(Last) := The_String(J);
        END IF;
     END LOOP;
     RETURN Result(Result'First .. Last);
  END Strip;


  Input: File_Type := Ada.Text_IO.Standard_Input;

BEGIN

   WHILE NOT End_of_File(Input) LOOP
     DECLARE 
     Line : String := Ada.Text_IO.Get_Line(Input);
     StrippedLine : String := line;
     ascii_val: Integer :=0;

  BEGIN
     StrippedLine := Strip(Line, "-");         
     ascii_val := integer'value(StrippedLine);
     Put(ascii_val);
     Put_line(StrippedLine);
  END;
   END LOOP;
   Close (Input);
end isbntest;

解决方案

The problem is that you're messing with the length of an array after you created it. Don't do that.

Instead of

  DECLARE 
     Line : String := Ada.Text_IO.Get_Line(Input);
     StrippedLine : String := line;
  BEGIN
     StrippedLine := Strip(Line, "-");   

Just initialise Stripped_Line directly to the correct size when you declare it.

  DECLARE 
     Line : String := Ada.Text_IO.Get_Line(Input);
     StrippedLine : String := Strip(Line, "-"); 
  BEGIN

I'm assuming your "Strip" function works correctly here..

这篇关于Ada-提出了Constraint_error:“值:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆