That's funny. I would have expected a result of 3100, not 3101.
Anyway, the problem has to do with your use of StrNCopy. Please
remember that this function does not necessarily store a NULL
terminator. Thus, the 3rd zero from 4000 is still in the
"dollarsString" buffer, turning your "100" into "1000".
Try:
MemCopy (dollarsString, string, dollarLength);
dollarsString [dollarLength] = 0;
-- Keith
At 12:56 PM -0700 10/24/01, Edward VanVliet wrote:
>What is the best practice for string manipulation?
>
>I have a function called Look4Dollars that scans a string for the decimal
>point, copys the dollar portion of the string, converts it to an int and
>adds it to a total.
>when I call it in the following sequence:
>
>Look4Dollars ("3000.00");
>Look4Dollars ("100.00");
>
>The resulting total becomes 4000 instead of 3101.
>
>I am stumpped!
>
>// Global variables
>char * dollarsString[6];
>char totalDollarsString[8];
>Int32 dollars;
>Int32 totalDollars;
>
>
>static void Look4Dollars( char * string)
>{
>char * decimalPlace;
>Int32 dollarLength;
>
>
> decimalPlace = StrStr(string, ".");
> if (decimalPlace != NULL)
> {
> dollarLength = decimalPlace - string;
> StrNCopy(dollarsString, string, dollarLength);
> dollars = StrAToI(dollarsString);
> totalDollars = totalDollars + dollars;
> StrIToA(totalDollarsString, totalDollars);
> }
>}
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/