![]() |
|
Displaying an int (or number of any type) using a draw comand? |
Durnus
Member #7,997
November 2006
![]() |
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
![]() |
theres sprintf (and use with allegros textout_ex) from libc, and textprintf_ex from allegro. Also theres atoi and atol from libc as well. -- |
Durnus
Member #7,997
November 2006
![]() |
Thomas Fjellstrom said: Also theres atoi and atol from libc as well.
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
![]() |
Quote: What is atoi? What is atol?
Some sample code:
Hope that helps... -- |
Matthew Dalrymple
Member #7,922
October 2006
![]() |
Durnus said:
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. The printf() family are meant for formatting strings that are dependent on outside sources such as a variable. Learn them, use them, love them. =-----===-----===-----= |
Don Freeman
Member #5,110
October 2004
![]() |
to: Matthew Dalrymple Edit:
-- |
Matthew Dalrymple
Member #7,922
October 2006
![]() |
:-P Alright, you got me... OWNED ... int a = 450; char b[480]; sprintf(b, "a is %i", a);
=-----===-----===-----= |
Don Freeman
Member #5,110
October 2004
![]() |
Sorry...just nit picking...I know. Edit: -- |
Durnus
Member #7,997
November 2006
![]() |
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.
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
|
Thomas Fjellstrom
Member #476
June 2000
![]() |
I think you need to spend some more time learning C a C string is just an array of char. basic C concept. So is libc. -- |
Don Freeman
Member #5,110
October 2004
![]() |
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... -- |
Durnus
Member #7,997
November 2006
![]() |
-> Don Freeman - Oh, okay. I though you were calling me a newb. 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.
|
Thomas Fjellstrom
Member #476
June 2000
![]() |
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). -- |
Don Freeman
Member #5,110
October 2004
![]() |
Seriously....very easy...
I don't know how to make it any easier for you...sorry.:'( -- |
Fladimir da Gorf
Member #1,565
October 2001
![]() |
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
![]() |
Durnus said:
I'm doing debugging. How do you display an integer value on the screen?
Actually, if ALL you want to do is display the value of an integer on the screen, then
You don't NEED to convert it to a string! -- |
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
![]() |
Actually, Don, using allegro it would be textprintf_ex |
Thomas Fjellstrom
Member #476
June 2000
![]() |
%d and %i are identical afaik. for floats and doubles you use %f. -- |
LennyLen
Member #5,313
December 2004
![]() |
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(). 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.
|
|