PDA

View Full Version : How to write number bigger than 9?



wzhao53
11-29-2003, 01:31 PM
I used following command to write number and character on the graphic:
glutBitmapCharacter(GLUT_BITMAP_8_BY_13,c[i][j]+48); .
As you can see, if the value in c[i][j] is bigger than 9, it prints out those special characters and ABCD.....

How can I write something like 100 or 15?

Thanks!

Ostsol
11-29-2003, 02:05 PM
Just break up the number into its component digits.

Untested code:




#define MAX_DIGITS 10;

int anDigits[MAX_DIGITS] = {0};

for (int i = (MAX_DIGITS - 1); i >= 0 && nNum != 0; i--)
{
anDigits[i] = nNum % 10;
nNum /= 10;
}


Then start rendering digits from the first one that isn't zero.

EDIT: Here's a sample run through:

456 % 10 = 6
456 / 10 = 45
45 % 10 = 5
45 / 10 = 4
4 % 10 = 4
4 / 10 = 0

You're left with the digits 6, 5, and 4. Since were filling up the array from the back, the digits are read in the order: 4, 5, 6.

You could use a stack, if you wanted, to contain the digits. That way you just push each digit as you figure it out then pop them when you want to render. That way, you'd be easily getting them out of the container in the right order.

[This message has been edited by Ostsol (edited 11-29-2003).]

[This message has been edited by Ostsol (edited 11-29-2003).]

Nutty
11-29-2003, 05:37 PM
Why not just;




char buffer[32];
sprintf(buffer, "%d", num);


Then you have a string of the digits.. easy.

Ostsol
11-29-2003, 06:39 PM
. . .

Somehow I always manage to think of overly complex solutions to simple problems. . . :p

maximian
11-29-2003, 07:41 PM
That is what came to my mind also.

I always remember the lines from the STNG episode:

Geordi: "Scotty, that is crazy"
Scotty: "I have spent my whole life thinking up ways to do things crazy"