Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Displaying an int (or number of any type) using a draw comand?

This thread is locked; no one can reply to it. rss feed Print
Displaying an int (or number of any type) using a draw comand?
Durnus
Member #7,997
November 2006
avatar

I'm doing debugging. How do you display an integer value on the screen?

I'm looking for a specific command, or a way to change an int to a string.

Also, what do I initialize strings as? ???

Thomas Fjellstrom
Member #476
June 2000
avatar

theres sprintf (and use with allegros textout_ex) from libc, and textprintf_ex from allegro. Also theres atoi and atol from libc as well.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Durnus
Member #7,997
November 2006
avatar

Thomas Fjellstrom said:

Also theres atoi and atol from libc as well.

??? What's atoi? What's atol?

I know how to print strings, I just need to convert an int to a string.

What library do I include to use strings, and how do I convert them?

Don Freeman
Member #5,110
October 2004
avatar

Quote:

What is atoi? What is atol?

1atof, atoi, atol
2Convert strings to double (atof), integer (atoi), or long (atol).
3 
4double atof( const char *string );
5int atoi( const char *string );
6long atol( const char *string );
7 
8Routine Required Header Compatibility:
9atof <math.h> and <stdlib.h> ANSI
10atoi <stdlib.h> ANSI
11atol <stdlib.h> ANSI
12 
13Libraries:
14LIBC.LIB Single thread static library, retail version
15LIBCMT.LIB Multithread static library, retail version
16 
17Return Value:
18Each function returns the double, int, or long value produced by interpreting the
19input characters as a number. The return value is 0 (for atoi ), 0L (for atol), or
200.0 (for atof) if the input cannot be converted to a value of that type. The return
21value is undefined in case of overflow.
22 
23Parameter:
24string String to be converted

Some sample code:

1/* ATOF.C: This program shows how numbers stored
2 * as strings can be converted to numeric values
3 * using the atof, atoi, and atol functions.
4 */
5 
6#include <stdlib.h>
7#include <stdio.h>
8 
9void main( void )
10{
11 char *s; double x; int i; long l;
12 
13 s = " -2309.12E-15"; /* Test of atof */
14 x = atof( s );
15 printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );
16 
17 s = "7.8912654773d210"; /* Test of atof */
18 x = atof( s );
19 printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );
20 
21 s = " -9885 pigs"; /* Test of atoi */
22 i = atoi( s );
23 printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );
24 
25 s = "98854 dollars"; /* Test of atol */
26 l = atol( s );
27 printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
28}
29 
30Output:
31atof test: ASCII string: -2309.12E-15 float: -2.309120e-012
32atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210
33atoi test: ASCII string: -9885 pigs integer: -9885
34atol test: ASCII string: 98854 dollars long: 98854

Hope that helps...
You should know how to use that to work with allegro... ::)

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Matthew Dalrymple
Member #7,922
October 2006
avatar

Durnus said:

??? What's atoi? What's atol?

atoi() is a char to an integer converter. atol() is a char to a long conversion. To convert an int to a string do this:

...
int a = 450;
sprintf("a is %i", a);

OUTPUT: a is 450

Reference: http://www.cplusplus.com/ref/cstdio/sprintf.html

Like Thomas said, you can use the built in allegro functions like textout_ex and stuff, they are around the same format as the printf() family.
You use %C where C being the character represented by the type of variable used. Use that website above to look at the different characters you can use.

The printf() family are meant for formatting strings that are dependent on outside sources such as a variable. Learn them, use them, love them. :-X

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

Don Freeman
Member #5,110
October 2004
avatar

to: Matthew Dalrymple
Your reference does not match your code:
int sprintf ( char * buffer, const char * format [ , argument , ...] );
is what I got from that...I don't see the buffer you are storing your output in...;D

Edit:
I myself failed him! I didn't notice he wanted to convert TO string not FROM string...
Sorry...use:

1_itoa()
2Convert an integer to a string.
3char *_itoa( int value, char *string, int radix );
4 
5// Sample code:
6/* ITOA.C: This program converts integers of various
7 * sizes to strings in various radixes.
8 */
9 
10#include <stdlib.h>
11#include <stdio.h>
12 
13void main( void )
14{
15 char buffer[20];
16 int i = 3445;
17 long l = -344115L;
18 unsigned long ul = 1234567890UL;
19 
20 _itoa( i, buffer, 10 );
21 printf( "String of integer %d (radix 10): %s\n", i, buffer );
22 _itoa( i, buffer, 16 );
23 printf( "String of integer %d (radix 16): 0x%s\n", i, buffer );
24 _itoa( i, buffer, 2 );
25 printf( "String of integer %d (radix 2): %s\n", i, buffer );
26 
27 _ltoa( l, buffer, 16 );
28 printf( "String of long int %ld (radix 16): 0x%s\n", l,
29 buffer );
30 
31 _ultoa( ul, buffer, 16 );
32 printf( "String of unsigned long %lu (radix 16): 0x%s\n", ul,
33 buffer );
34}
35 
36 
37Output:
38String of integer 3445 (radix 10): 3445
39String of integer 3445 (radix 16): 0xd75
40String of integer 3445 (radix 2): 110101110101
41String of long int -344115 (radix 16): 0xfffabfcd
42String of unsigned long 1234567890 (radix 16): 0x499602d2

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Matthew Dalrymple
Member #7,922
October 2006
avatar

:-P Alright, you got me... OWNED
EDITED CODE:

...
int a = 450;
char b[480];
sprintf(b, "a is %i", a);

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

Don Freeman
Member #5,110
October 2004
avatar

Sorry...just nit picking...I know. :) The above fixes are ok... I think what I edited in my post above is what he needs...::)

Edit:
The radixes value above is what number system to use:
such as base 2(binary, base 10(decimal), base 16(hex),ect...

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Durnus
Member #7,997
November 2006
avatar

Sorry, but I have no clue about strings. I'm fine with arrays and integers and stuff, but I learned from a tutorial that didn't tell me any of this.

:P None of this has really helped me, just confused me.

In another language it was as easy as (string)integer_value.

Also, nobody has told me how to initialize strings.

Let me re-state my questions: Converting integers to strings. What to include to be able to make strings.

Don Freeman said:

You should know how to use that to work with allegro...

>:(

http://freewebs.com/sandmans_dream<- mine.

EDIT: Dang, three posts went by when I was writing this. Let me look at them...

EDIT2: Hmm... I couldn't really comprehend that code. I don't like using what I can't understand (I can't write it in a mini-function in my code), so could I have a little explanation on what printf is and all those %s things are? Thanks for all the help though :P

Thomas Fjellstrom
Member #476
June 2000
avatar

I think you need to spend some more time learning C :P

a C string is just an array of char. basic C concept. So is libc.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Don Freeman
Member #5,110
October 2004
avatar

I didn't mean it like that...I mean that with what I showed you, you should be able to use it with allegro to display it on the screen... :P

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Durnus
Member #7,997
November 2006
avatar

-> Don Freeman - Oh, okay. I though you were calling me a newb. :P

Jeez... I thought allegro was C++... >.<

I know about that list of char C idea thing, but I have no clue what this code it doing to it.

Me like C++. Me no like C.

In fact, I'm giving up on this. I'm just not gonna debug my thing this way, and just try random things. :P Thanks for the help, but I don't think its gonna stick in my brain. :P

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

could I have a little explanation on what printf is and all those %s things are?

Im don't usually like it when people tell others to RTFM, but really, RTFM. And go search for some C tutorials that actually explain strings, and libc functions.

Oh, if you want C++, std::string is your friend. as is std::stringstream (at least I think thats what its called).

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Don Freeman
Member #5,110
October 2004
avatar

Seriously....very easy...

1#include <stdlib.h>
2#include <stdio.h>
3#include <memory.h>
4void main( void )
5{
6 char buffer[20]; // make sure to set the array big enough to hold the integer...
7 // remember that signed integers can range from (-2,147,483,648) through
8 // (2,147,483,647)...just make sure not to run over the buffer...
9 // to zero out the buffer use:
10 memset(&buffer,'\0',sizeof(buffer));
11 int i = 3445;
12 _itoa( i, buffer, 10 ); // you want to get an string that contains the integer i
13 // in base 10(decimal) format...stored in buffer
14 printf( "String of integer %d (radix 10): %s\n", i, buffer );
15}

I don't know how to make it any easier for you...sorry.:'(

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Fladimir da Gorf
Member #1,565
October 2001
avatar

Even easier:

stringstream s;
s << num;
cout << s.str();

and no Windows-style buffer overruns even if you're running a futuristic 256-bit computer.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Don Freeman
Member #5,110
October 2004
avatar

Durnus said:

I'm doing debugging. How do you display an integer value on the screen?
I'm looking for a specific command, or a way to change an int to a string.
Also, what do I initialize strings as?

Actually, if ALL you want to do is display the value of an integer on the screen, then
just do this:

// using allegro:
textprintf(dest_bitmap,font,text_x,text_y,text_color,"Value of integer=%i",integer);
// or console:
printf("Value of integer=%i\n",integer);

You don't NEED to convert it to a string! :P

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Dorianin
Member #8,013
November 2006

there's a thingee in the code snippets here that is what you want...i modified it to a class to accept any text input(int, char, etc...)and display it on the screen...its pretty cool...

never play leapfrog with a unicorn....

James Stanley
Member #7,275
May 2006
avatar

Actually, Don, using allegro it would be textprintf_ex :P
Also, shouldn't it be %d, not %i?

Thomas Fjellstrom
Member #476
June 2000
avatar

%d and %i are identical afaik. for floats and doubles you use %f.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

LennyLen
Member #5,313
December 2004
avatar

Quote:

Actually, Don, using allegro it would be textprintf_ex

If we're going to be picky, then it's only for later versions of Allegro that it's textprintf_ex(). :P

Quote:

%d and %i are identical afaik.

For textprintf/textprintf_ex there's no difference. This isn't always the case though.

My textbook (C Programming: A Modern Approach by K.N. King) has this to say on the matter:

Q: I've seen the %i conversion used to read and write integers. What's the difference between %i and %d?

A: When used in a printf format string, there's no difference. In a scanf string however, %d can only match an integer written in decimal (base 10) form, while %i can match an integer expressed in octal (base 8), decimal, or hexidecimal (base 16). If an input number has a 0 prefix (as in 056), %i treats it as an octal number; if it has a 0x or 0X prefix (as in 0x56), %i treats it as a hex number. Using %i instead of %d to read a number can have surprising results if the user should accidentally puts a 0 at the beginning of a number. Because of this trap, I recommend sticking with %d.

Go to: