New to Ada, noticing something strange.
- From: mike.martelli@xxxxxxxxx
- Date: 29 Sep 2005 10:20:42 -0700
Here is an excerpt of my code. Every function works as it should, the
program is set up correct, and the correct values are being passed to
all functions.
The idea of the program is perform arithmetic on arbitrarily long
integers that are inputted as strings using the paper and pencil
approach (digit by digit, from right from right to left, with a carry
digit). The user will enter a base, operand1, and operan2 via command
line arguments. (if you need more explanation let me know).
with Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.Command_Line;
with Ada.Strings.Unbounded;
with Ada.Characters.Handling;
use Ada.Text_IO;
use Ada.Integer_Text_IO;
use Ada.Command_Line;
use Ada.Strings.Unbounded;
procedure BigInts is
....
charMap: array (Character) of Integer;
charMap('0') := 0;
charMap('1') := 1;
charMap('2') := 2;
....
charMap('E') := 14;
charMap('F') := 15;
function C2C(strChar: String) return Character is
begin
Put_Line(strChar);
case Integer'Value(strChar) is
when 10 => return 'A';
when 11 => return 'B';
when 12 => return 'C';
when 13 => return 'D';
when 14 => return 'E';
when 15 => return 'F';
when others => return strChar(1);
end case;
end C2C;
function AddDigits(op1, op2: Character) return Character is
begin
charDigit:=C2C(Integer'Image(charMap(op1)+charMap(op2)+carry));
Put("Print1: ");
Put(C2C(Integer'Image(charMap(op1)+charMap(op2)+carry)));
if(charMap(op1)+charMap(op2)+carry)>(charMap(charBase)-1) then
Put_Line("here1: ");
--Set the digit to the highest possible values
charDigit:=C2C(Integer'Image(charMap(charBase)-1));
--carry is all of the rest
carry:=(charMap(op1)+charMap(op2)+carry)-(charMap(charBase)-1);
else
Put_Line("here2: ");
carry:=0; --there is no carry
end if;
Put("Digit: ");
Put(charDigit);
New_Line;
Put("Carry: ");
Put(carry);
New_Line;
return charDigit;
end AddDigits;
....
end BigInts;
(I removed some extra white space so it would align better in the post)
I have some Put()/Put_Line() statements in AddDigits() for debugging
purposes. The problem is in Convert2Character(), nothing was ever
being returned by that function. I added the Put_Line(strChar) to make
sure a correct value was being passed to it. Once I added that
Put_Line() a value was being returned by the function and it was
correct. It seems that a Put_Line() is needed for some reason or it
does not do what it is supposed to do.
.
- Follow-Ups:
- Re: New to Ada, noticing something strange.
- From: Jeffrey R. Carter
- Re: New to Ada, noticing something strange.
- Prev by Date: Re: String filtering
- Next by Date: Re: GNAT GPL - Anonymous Access Type
- Previous by thread: GNAT - filename/unitname question
- Next by thread: Re: New to Ada, noticing something strange.
- Index(es):
Relevant Pages
|