1

So I can easily grab the int value I am looking for from my .txt file through standard input but when there is a white space (or multiple white spaces) before it, it doesn't work and a data error occurs, Since its an integer though, my code below which works for a grabbing a string, does not work when I put an int in there because you cannot compare an Int to a String. Any ideas on workarounds?

 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;
user2855405
  • 495
  • 2
  • 7
  • 20
  • Check out the Look_Ahead procedure. If it shows you a whitespace character, Get that character (to a character variable). Keep doing that until you see a number (or + or -). But what I'd do is get the whole line as a string and break that up. If you know a slice of a string (like `Expression(Start ..End)` is an integer, then `int_variable := Integer'value(Expression(Start .. End);' will convert that slice to an integer using the "value attribute" –  Mar 01 '15 at 22:11
  • When I try and use look_ahead it says "missing argument for parameter End_OF_Line, context requires function call, found procedure name." – user2855405 Mar 01 '15 at 22:28
  • that's why I called it a procedure, not a function. It shouldn't be difficult to find documentation - http://www.adaic.org/resources/add_content/standards/05rm/html/RM-A-10-1.html for one. I recommend John Barnes book as a serious reference. –  Mar 01 '15 at 23:04
  • For more on using lookahead : http://www.radford.edu/~nokie/classes/320/proc.lookahead.html –  Mar 01 '15 at 23:09
  • 2
    `get` on an integer is supposed to skip leading blanks (see [RM A.10.8](http://www.ada-auth.org/standards/12rm/html/RM-A-10-8.html), paragraph 8), so I'm not clear on what your problem is. If you're doing something more complex than just reading integers from a file that are separated by spaces, then I agree with Brian's second sentence to read the entire line and work with it yourself. – ajb Mar 02 '15 at 00:34

2 Answers2

0

ajb was right integers already go the space checking, my code was only messing it up. No loop needed for getting ints but it works well for String's/Characters.

user2855405
  • 495
  • 2
  • 7
  • 20
0

So you can use the 'Value attribute to go from string to int and 'Image to go from int to string. These are attributes in Ada. Learning how to effectively use attributes will definitely increase your Ada productivity.

Basically I think this does what you want. You can read the string in and convert the string to an integer with

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;

It yields the output:

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

If you want help iterating on this idea and morphing your original question into a slightly different method, let me know. I will gladly work with you on this ;)

Billy Ferguson
  • 1,429
  • 11
  • 23