I'm getting a fatal exception when using the following function I wrote. I
was wondering if any of you guys and gals would notice anything wrong with
it. I'm not feeling 100% today, so goodness only knows what stupid logic I
wrote into this.
The function "GetDecFromString" just returns a struct from one of the
strings.
All it does it take in 2 strings with periods, converts them to integers,
does the math, and spits out a string result.
Thanks in advance.
// This is my decimal structure
typedef struct
{
UInt num;
UInt dec;
} decNum, *decNumPtr;
CharPtr AddDecimals(CharPtr str1, CharPtr str2, UInt places)
{
// Need to integrate limited decimal places in string
// Rounding as well...
decNumPtr dec1, dec2;
UInt d, result, length;
int x = 0, i = 0, j = 0;
CharPtr lstr, strResult;
// Get Decimal structure from Strings
dec1 = GetDecFromString(str1);
dec2 = GetDecFromString(str2);
// Find difference of decimals
d = dec1->dec - dec2->dec;
// Make numbers same length
if(dec1->dec > dec2->dec)
while(x < d)
{
dec2->num = dec2->num * 10;
x++;
}
else if(dec1->dec < dec2->dec)
while(x < d)
{
dec1->num = dec1->num * 10;
x++;
}
// Add two numbers together
result = dec1->num + dec2->num;
// Allocate memory for local string
lstr = MemPtrNew(sizeof(result));
// Allocate memory for result string + 1 for decimal
strResult = MemPtrNew(StrLen(lstr) + 1);
// Covert UInt to String
StrIToA(lstr, result);
// Get String Length
length = StrLen(lstr);
// Subtract length result by largest dec value
// Note: no need to add 1, because strings start at address 0
if(dec1->dec > dec2->dec)
length = length - dec1->dec;
else
length = length - dec2->dec;
// Copy String and insert decimal
while(lstr[i] != '\0')
{
if(i == length)
{
strResult[i] = '.';
i++;
}
else
{
strResult[i] = lstr[j];
i++;
j++;
}
}
// Free memory
MemPtrFree(lstr);
MemPtrFree(strResult);
return(strResult);
}
--
Tim Astle
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palm.com/devzone/mailinglists.html