0
votes

I am confused about changing types in Ada. Specifically receiving user input, which is stored as a string, and then performing operations on it as a float or integer. My goal is to make a table of logarithms based on a users start and stop points, incrementing by any value they want. I have tried something such as: Start := Float'Image(Start); I could you some help, thanks.

with Ada.Text_IO, Ada.Float_Text_IO, Ada.Numerics.Elementary_Functions;
use  Ada.Text_IO, Ada.Float_Text_IO, Ada.Numerics.Elementary_Functions;

procedure log_table is

   Start, Stop, Increment, temp : Float; 

begin                                           -- Prompt for input
   Put_Line("To print a table of logarithms,");
   Put(" enter the start, stop, and increment values: ");
   Get(Start); Get(Stop); Get(Increment);

   loop 
    exit when (Start > Stop);
    Put("The logarithm of");
    Put(Start);
    Put(" is ");
    Put_Line( log(Start, 10.0) );
    Start := Start + Increment;
   end loop;

end log_table;

So the code now runs... I will post the changed portion of code below. All I did was change the Put_Line function to a Put function and then added a New_Line function after. Any reason why this works? From my understanding the only difference between Put_Line and Put is that is adds a '\n' to the end. A better understanding would be appreciated.

   loop 
    exit when (Start > Stop);
    Put("The logarithm of ");
    Put(Start);
    Put(" is ");
    Put( log(Start, 10.0));
    New_Line;
    Start := Start + Increment;
   end loop;
1
Do yourself the favour of reading the specification of the packages you are using. If you remove the use clause, and insert the full names of the subprograms you call, you might also learn a bit.Jacob Sparre Andersen
It's really simple, Put_Line doesn't exist for Real types as stated in the ARMFrédéric Praca
Put_Line does not add '\n' to the end. ('\n' is a syntax error in Ada.) If you Put a String containing "\n", you'll get the 2 characters '\' and 'n' in your output.Jeffrey R. Carter

1 Answers

4
votes

In general you convert from one type to another using the name of the target type as if it was a function name:

 A := Some_Integer_Type (3.41);

But this only works for compatible types. Arrays (such as strings) and numerical types are never compatible, so you are most likely looking for LRM 3.5(52).