1

所以我可以通过标准输入轻松地从我的 .txt 文件中获取我要查找的 int 值,但是当它之前有一个空格(或多个空格)时,它不起作用并且发生数据错误,因为它是整数虽然,我下面的代码用于抓取字符串,但当我将 int 放在那里时不起作用,因为您无法将 Int 与 String 进行比较。关于解决方法的任何想法?

 Until_loop:                 -- Loop to get chars even if space is between
       loop 

         get(int_variable);

     exit Until_loop when int_variable) /= " ";   --won't work

     end loop Until_loop;
4

2 回答 2

0

ajb 是正确的整数已经进行了空间检查,我的代码只是把它搞砸了。获取整数不需要循环,但它适用于字符串/字符。

于 2015-03-02T00:54:49.423 回答
0

所以你可以使用 'Value 属性从字符串到 int 和 'Image 从 int 到字符串。这些是 Ada 中的属性。学习如何有效地使用属性肯定会提高你的 Ada 生产力。

基本上我认为这可以满足您的需求。您可以读取字符串并将字符串转换为整数

with Ada.Text_IO; use Ada.Text_IO;
procedure Foo is
    Str: String(1 .. 10) := (others => ' ');
    Last_Char : Integer;
begin
    Put("Enter num: ");
    Get_Line(Str, Last_Char);
    Put_Line("Str = """ & Str & """");
    Put_Line("Last = " & Integer'Image(Last_Char));
    Put_Line("The num is " & Integer'Value(Str));
end Foo;

它产生输出:

Enter num: 1239
Str = "1239      "
Last_Char =  4
The num is 1239

如果您需要帮助迭代这个想法并将您的原始问题转变为稍微不同的方法,请告诉我。我很乐意与您合作;)

于 2015-05-08T14:13:41.170 回答