Quick question!  I apologize to have to bother everyone again, but this is
really throwing me off!

Alright this function works, except I'm running into a problem with the
string being returned.  It adds the 2 strings together and writes out the
correct number, however when I move back and forth between forms, the answer
sometimes changes each time the function is called again.

Example:
(correct answer to addition: 130.704)
First time form is loaded:  130.704
Second time form is loaded:  130.70404 (It seems the string is sometimes not
putting a nul at it's end???)
Third time loaded 130.704
Forth time loaded 130.704
Fifth time loaded 130.7040404
*Generally the number output is correct, yet it seems to be written wrong.*

Now in the below function I've selected the part that I believe is the
trouble zone.  I believe it has to do with the string memory allocation.
Does anyone see anything wrong?


/***********************************************************************
 *
 * FUNCTION:    AddDecimals()
 *
 * DESCRIPTION: This routine adds 2 Strings together
 *
 * PARAMETERS:  - str1
 *    - str2
 *    - places
 *
 * RETURNED: CharPtr
 *
 ***********************************************************************/
CharPtr AddDecimals(CharPtr str1, CharPtr str2, UInt places)
{
     // Need to integrate limited decimal places in string
     // Rounding as well...

     decNum dec1; // Decimal structures
     decNum dec2;
     CharPtr lstr;  // Strings
     CharPtr strResult;
     long int result; // Addition result
     long int temp;  // used to find length of result
     int counter = 1;   // Length of int
     int x, i = 0, j = 0; // Counters
     Boolean cont = true;

     // Get Decimal structure from Strings
     GetDecFromString(str1, &dec1);
     GetDecFromString(str2, &dec2);

     // Make numbers same length
     if(dec1.dec > dec2.dec)
          for(x = 0; x < (dec1.dec - dec2.dec); ++x)
               dec2.num = dec2.num * 10;

     else if(dec1.dec < dec2.dec)
          for(x = 0; x < (dec2.dec - dec1.dec); ++x)
               dec1.num = dec1.num * 10;

     // Get the sum
     result = dec1.num + dec2.num;

     // Count # of places it occupies
     for(temp = result; temp > 0; temp = temp / 10)
          counter++;

/*******************************************/
     // Allocate memory for local string
//     lstr = (CharPtr)MemPtrNew(counter);
//     strResult = (CharPtr)MemPtrNew(counter + 1);
/*******************************************/

/* I've tried hardcoding this (below) and it seems more solid that my first
dynamic approach (above) */
/* The first way (above) tends to show memory, and tends to lead to a
DmWriteCheck error now and then. */
/* Below just repeats the last few numbers of an answer when I call the
function the 2nd, 3rd, etc. times. Not finding the current nul?  I don't
know! */

/*******************************************/
     // Allocate memory for local string
     lstr = (CharPtr)MemPtrNew(20);
     strResult = (CharPtr)MemPtrNew(21);
/*******************************************/

     // Covert UInt to String
     StrIToA(lstr, result);

     // Subtract length result by largest dec value
     if(dec1.dec > dec2.dec)
          counter = counter - dec1.dec;
     else if(dec1.dec < dec2.dec)
          counter = counter - dec2.dec;

     // Copy String and insert decimal
     while(lstr[i] != '\0')
     {
          if((i == (counter - 1)) && (cont == true))
          {
               strResult[j] = '.';
               j++;
               cont = false;
          }
          else
          {
               strResult[j] = lstr[i];
               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

Reply via email to