I would like to get all the elements of a string array and I just get the last one followed by a "zero" element.

void DrawFonts(int num, int x, int y)
    Char Buffer [20];
    CString ss;
    num = 15; //or whatever more than 2 elements

    itoa( num, Buffer, 10 );

    int MyLen = strlen(Buffer);

    myrect.top      = 0; 
    myrect.bottom = 17;

    for(i=0; i< MyLen; i++){

    int A = (int) atoi (Buffer);

    myrect.left   =     A * 15;
    myrect.right  = (A+1) * 15;
    g_pDisplay->Blt( x+i *15, y, g_pFontSurface, &myrect ); 

Will someone help me to get the full element list?
Thank you all in advance.

Ioannis Goulatis

commented: Use code tags. +0

I Forgot to mention that I am using the method atoi. Is there any other method to implement this?

Also I am working with Microsoft Visual C++.

I Did it myself at last!!!!

void DrawFonts(int num, int x, int y , bool center)
    //num = 15 ;
    char* pMessage="";
    int MyLen = strlen(pMessage);

    char* pEncoded =  new char[MyLen];
    //char* pDest    =  pMessage;//pEncoded;

    myrect.top    = 0;
    myrect.bottom = 17;

    if(center) x = x - MyLen*7;

    int step=0;
    // pMessage=0;
    for(i=0; i<MyLen; i++)
    pEncoded[i]  = *pMessage;
    step = atoi(&pEncoded[i]);

    myrect.left   =  step*15;
    myrect.right  = (step+1)*15;

    g_pDisplay->Blt(x+i*15, y, g_pFontSurface, &myrect );

If there is another way of doing this, I would like to know it.

There are some other problems here:

char* pMessage="";  // here you define a POINTER, not space for it to point to.

Here you've allocated a POINTER to a charactor string and set the pointer to the location of an empty string the compiler builds for you. Then you proceed to overwrite the location of the empty string and whatever is after it with some number of digits. It should be something like this:

char message[16];  // here you set aside 16 bytes for the message
_itoa(num, message, 10);

Here's a minor nit:

if(center) x = x - MyLen*7;

I'm assuming that 7 is the point width of your font? And it is not a proportional font? There are routines that can tell you the exact width of a set of chars in any given font, which take into account the variable widths of chars. At the very least, you might define a constant of something like 'kAverageFontWidth = 7' so it can be easily changed.

I'm sorry to be dense, but what are you attempting here?

pEncoded[i] = *pMessage;
step = atoi(&pEncoded[i]);

What you are doing (assuming you fix the problem that pMessage points to random space) is setting step to num, num - leading digit, num - 2 leading digits, and so on. Is that what you want?

So, if num is 1234, step will be 1234, then 234, then 34, then 4.

Dear Chainsaw,

First of all I would like to thank you for your time and your ideas. The part I implemented works fine till now with no problems at all, but I will work on your sugestions hopping to see any better results. :)