Displaying Integers with OpenGL

I'm having trouble using opengl to display integers (it works only if they're 1-digit). I'm using glutStrokeCharacter in a function I found online to display them and I function that I created myself to convert them to char* (The one I made works perfectly with console applications).Some of the code I'm using is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void drawStrokeText(char* string, float scale)
{
	  char *c;

      glScalef(scale,scale,scale);
	  for (c=string; *c != '\0'; c++)
	  {
    		glutStrokeCharacter(GLUT_STROKE_ROMAN , *c);
	  }
}

...

char* IntToChar(int number)
{
    ostringstream msg;
    msg<<number;
    string message = msg.str();

    char numberS[message.length()+1];
    for (int x=0;x<message.length()+1;x++)
    {
        numberS[x] = message[x];
    }

    return numberS;
}

...

drawStrokeText(IntToChar(score),Normal_Text);


The program is displaying @%i instead of 10 (the value stored in score). Thx in advance for any help.
Function IntToChar is returning a pointer to a local object. Don't do it, as the local object goes out of scope when the function returns, and the memory it occupied may be re-used by some other process.

Try this. You will need to allocate the character buffer with sufficient space before calling the function.

1
2
3
4
5
6
void IntToChar(int number, char * buf)
{
    ostringstream msg;
    msg << number;
    strcpy(buf, msg.str().c_str());
}

Thanks a lot. It works perfectly now.
Topic archived. No new replies allowed.