Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Translating C++ FOR cycle into Foxpro code
Message
From
05/12/1997 20:45:16
 
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00064105
Message ID:
00064209
Views:
43
You have 2 problems:

1. In C, char strings (and arrays in general) start at 0 and end at length - 1. So, you need to correct your loop. What you have in C must be translated in FP by a loop from LEN(yourstring) to 1.
2. You don't give the C declarations for rhs and digit variables. From what this code is doing, I believe they are of some integer type. If I'm right, then your formula must be:

rhs = rhs + int((2 * digit)/10) + ((2 * digit) % 10)

because in VFP the result of the division is not truncated by default (in C, if all operands are of integer type, the result is also an integer).

BTW, I don't see any meaning to loop descendant in this case. Anyway, you can use a FOR command. It's much faster (also more clear) than DO WHILE.

Vlad

>Hi everyone, I've been cracking my head trying to translate this cycle and into FP code; so far I haven't had any luck. If anybody can, please help.
>
>*Sample value:
>tempCustStr = '50400000019'
>
>****C++ code:******
>rhs = 0
>for (i = strlen(tempCustStr)-1;i>=0;i--){
> digit = *(tempCustStr + i) - '0'
> if (i % 2 == 0) {
> rhs += digit;
> //odd digits
> } else {
> rhs += (2 * digit) /10 + (2 * digit) % 10;
> }
>}
>
>rhs = 99 - rhs
>checkdigit = rhs / 10
>lastdigit = rhs % 10
>
>****My FP code: (It's not working properly)******
>rhs = 0
>i = LEN(tempCustStr) - 1
>DO WHILE i >= 0
> digit = VAL(SUBST(tempCustStr,i,1))
> IF i % 2 = 0
> rhs = rhs + digit
> ELSE
> rhs = rhs + ((2 * digit)/10) + ((2 * digit) % 10)
> ENDIF
> i = i - 1
>ENDDO
>
>rhs = 99 - rhs
>checkdigit = rhs /10
>lastdigit = rhs % 10
>
>Thanks a lot, I really appreciate any help
>
>Have a good day,
Previous
Reply
Map
View

Click here to load this message in the networking platform