comp.lang.ada
 help / color / mirror / Atom feed
From: mike.martelli@gmail.com
Subject: New to Ada, noticing something strange.
Date: 29 Sep 2005 10:20:42 -0700
Date: 2005-09-29T10:20:42-07:00	[thread overview]
Message-ID: <1128014442.343461.185800@g49g2000cwa.googlegroups.com> (raw)

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.




             reply	other threads:[~2005-09-29 17:20 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-29 17:20 mike.martelli [this message]
2005-09-29 18:39 ` New to Ada, noticing something strange Jeffrey R. Carter
2005-09-29 19:05   ` mike.martelli
2005-09-29 22:25     ` Randy Brukardt
2005-09-29 23:58       ` mike.martelli
2005-09-30  0:28         ` mike.martelli
2005-09-30  6:06         ` Jeffrey R. Carter
2005-09-30  6:28         ` Jeffrey R. Carter
2005-09-30 10:19         ` Georg Bauhaus
2005-09-30 16:43           ` mike.martelli
2005-09-30  6:23     ` Jeffrey R. Carter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox