1. ## C++ Programming Question

Hi, im coding in c++ but i found a problem i cant get a thing to work

i need to get the len of the serial if its less than 10 i add a "9" to the start of it, this was what i could do

length1=strlen(heh);
while (length1 != 10)
{
heh[length1]+=0x39;
length1++;
}

but this only add one "9" at the end of the string, the rest is filled with shit, if anyone can help.

tnx
BGI 5x

2. Hiya BGI 5X,

From your description, I'm not entirely sure what the serial algo is but I can explain why your code isn't working.

If you're dealing with a condition where the serial length really must be "less than" 10, using the "not equals" operator is ambiguous and fails to correctly describe the situation. Whether the length is 1 or 100 it still remains != 10.

while(length1 != 10)
should be
while(length1 < 10)

Next, look at heh[length1] += 0x39;
length1++;

This explains your problem of 1 '9' at the end of the string and shit in the rest of it.

Length1, being the length of the string, will start to index the heh array at the last element of the string (which explains the '9'at the end of the string). Then, presumably because the array size of heh is longer than the actual string length, length1++ proceeds to index past the last element in the string into the remainder of the array. If you did not initialize the contents of the array when you delcared it, you will now be adding 0x39 to whatever crap is already in those memory locations beyond your string resulting in the shit of which you speak. I should also mention that if your serial length were to be greater than 10, the length1 variable would never == 10 and terminate the while loop... Infinite Loop == crash

From your description, I'm not entirely sure of the serial algo. Are you adding the ascii value for 9 (0x39) to the existing ascii values of each element in the string or are you simply adding 9 (0x09)to the existing ascii values for the string elements. For the sake of example I'm going to assume you're adding 0x39. Also for this situation I would suggest a 'for' loop rather than a 'while' loop.

Example: If the serial length is less than 10 add 0x39 to each element in the string starting with the first element.

length1=strlen(heh);
if(length1 < 10)
{
for(int i=0; i<length1; i++)
heh[i]+=0x39;
}

Anyway, I hope this clears up a few of the bugs If you post more details of the algorithm you're trying to code, I'm sure either myself or someone else around here can help you with your C.

Regards,
Clandestiny

3. hmmm :-)

I don't know sh*t about C++, but my guess is you confuse C++ strings with C-style strings (character arrays actually)... If you declared heh as a char* or char[] (which seems to be the case, since you're using strlen()), then AFAIK, the snippet you gave is C, not C++. So the += operator is the plus assignment, not the concat operator ;-) That's why you see these strange things, explained by Clandestiny

Use concat() with c-style strings, and += with the C++ datatype string, but don't mix them...

4. hmmm :-)

I don't know sh*t about C++, but my guess is you confuse C++ strings with C-style strings (character arrays actually)... If you declared heh as a char* or char[] (which seems to be the case, since you're using strlen()), then AFAIK, the snippet you gave is C, not C++. So the += operator is the plus assignment, not the concat operator ;-) That's why you see these strange things, explained by Clandestiny

Use concat() with c-style strings, and += with the C++ datatype string, but don't mix them...

5. Hello All

here is a resume

the program makes a checksum with name this checksum is X chars
suppose X = 6 chars (ABCDEF)
then the program will add a 0x39 at the start of ABCDEF until it gets 10
so in this case it'll be 9999ABCDEF

Clandestiny, I tried using your code and it didnt work, then i debugged it and say that it was adding 0x39 to the already made checksum :/

maybe this is coz what m0sk said about heh[i]+

btw Im using visual c++ 6.0.

if you didnt understand the algo i can explain again.

thanks
BGI 5x

6. non-optimised 'C' program :

#include <stdio.h>
#include <string.h>

int main(void)
{
char szSerial[50];
char szBuffer[50];

printf("Serial : ");
gets(szSerial);

while (strlen(szSerial)<10)
{
strcpy(szBuffer,"9");
strcat(szBuffer,szSerial);
strcpy(szSerial,szBuffer);
}

printf("New serial = %s",szSerial);
getchar();

return 0;
}

Wizard

7. yes, thats it Wizard, thanks a lot.
tnx all that helped

8. Two Lines:

for(int i=0; i<(10-OldSerialLength);NewSerial[i++]);
for(int j=0; NewSerial[i++]=OldSerial[j++];

9. anon (08-27-2001 22:14):
Two Lines:

for(int i=0; i<(10-OldSerialLength);NewSerial[i++]);
for(int j=0; NewSerial[i++]=OldSerial[j++];
oops last statement in first for loop needs to be

" NewSerial[i++]='9' "

10. for(int i=0; i<(10-OldSerialLength);NewSerial[i++]='9');
for(int j=0; j<OldSerialLength+1;NewSerial[i++]=OldSerial[j++]);

... would be better

Wizard

11. Were did my "i" go?

need to declare i outside of first loop cause its referanced in second

int i=0;
while( i<(10-OldSerialLength);NewSerial[i++]='9');
for(int j=0; j<OldSerialLength+1;NewSerial[i++]=OldSerial[j++]);

12. ...same routine...loop 2 is within loop 1 ....i is considered local for both loops (nested for loops)

13. "need to declare i outside of first loop cause its referanced in second"

Wrong, you don't *need* to : it depends on your compiler settings.

Anon (corrected) code perfectly works with VC++ default settings.

Wizard

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•