Announcement

Collapse

New Sub-Forum

In an effort to help make sure there are appropriate categories for topics of discussion that are happening, there is now a sub-forum for databases and database programming under Special Interest groups. Please direct questions, etc., about this topic to that sub-forum moving forward. Thank you.
See more
See less

translate a piece of c++ code to basic

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • translate a piece of c++ code to basic

    Can anybody translate this small C++ function into Basic syntax
    not using pointers (I've got powerbasic v2.1).
    *****************************************************************
    void MakeCheckDigit (charr *cString, int nLength, char *c)
    {
    int nArray[60], n0, n1 = 0, n2 = 0;
    for (n0 = 0; n0 < nLength; n0++){
    nArray[n0] = *(cString + n0) - '0'
    if ((n0 % 3) == 1 nArray[n0] = nArray[n0] * 3;
    if ((n0 % 3) == 2 nArray[n0] = nArray[n0] * 7;
    nArray[n0] = (nArray[n0] / 10) + (nArray[n0] % 10);
    n1 += nArray[n0];
    }
    n2 = n1 % 10;
    *c = '0' + n2;
    }
    ****************************************************************

    ------------------

  • #2
    I am not a C++ expert, but I have fooled around with it awhile ago.

    In order to switch from C++ to BASIC, some constructs had to be changed.
    The Array of Characters was replaced with a "Working Value" Integer.

    I would consider changing it to a String function, pass just the string,
    calculate nLength% as LEN(cString), do the calculations, and pass
    c$ as the Value of the Function.
    ie. FUNCTION MakeCheckDigit$(cString$) ... FUNCTION = c$ ... END FUNCTION

    Code:
    SUB MakeCheckDigit(cString$, nLength%, c$ )
      LOCAL workValue%, n0%, n1%, n2%
      n1% = 0 : n2% = 0
      FOR n0% = 1 TO nLength%
        workvalue% = VAL(MID$(cString$, n0%, 1))
        IF (n0% MOD 3) = 1 THEN
          workvalue% = workvalue% * 3
        ELSEIF (n0% MOD 3) = 2 THEN
          workvalue% = workvalue% * 7
        END IF
        workvalue% = (workvalue%/10) + (workvalue% MOD 10)
        n1% = n1% + workvalue%
      NEXT n0%
      n2% = n1% MOD 10
      c$ = "0" + LTRIM$(STR$(n2%))
    END SUB
    Regards,

    ------------------
    [email protected]
    :) IRC :)

    Comment

    Working...
    X