|
|
| Displaying ints |
|
demiselegion
Member #9,036
September 2007
|
How do I go about displaying an integer on the screen? Thanks, |
|
StevenVI
Member #562
July 2000
|
Hello, Me. If you want to display an integer to a console, use the printf function like so: #include <stdio.h> int main(int argc, char *argv[]) { int i=543; printf("i=%d",i); return 0; } If you are using C++, you also can use the cout function, though to me it is less intuitive: #include <iostream> int main(int argc, char *argv[]) { int k=404; cout << "k=" << k << endl; return 0; } If you want to print it to a bitmap in Allegro (such as the screen bitmap), you would do this: int j=1234; textprintf_ex(screen,font,0,0,makecol(255,255,255),-1,"j=%d",j); As always, be sure to read the docs. Follow the links in the code snippets. (Edited to include the C++ example.) __________________________________________________ |
|
Ron Novy
Member #6,982
March 2006
|
The awesome thing about this site is that when you post code... It highlights the allegro functions and when you click on them it brings you right to the information you need for that function... Genius ---- |
|
Mark Oates
Member #1,146
March 2001
|
Beautiful work, Harry. -- |
|
bamccaig
Member #7,536
July 2006
|
Harry Carey said: If you are using C++, you also can use the std::cout object, though to me it is less intuitive...
Fixed. I can't say I prefer the C nor C++ style of output. Both have their advantages and disadvantages. I find std::cout easier to use when I'm just printing values for debugging, etc. However, if I am stringing together a number of values the C-style functions better illustrate the result. /* * It might be because I learned of std::cout before printf, but I find * std::cout faster when just debugging or printing a simple value. */ std::cout << "i=" << i << std::endl; printf("i=%d\n", i);
/* * When printing a more complex string, I find printf better illustrates * the result. */ std::cout << iMonth << "/" << iDay << "/" << iYear << " " << iHour << ":" << iMin << ":" << iSec << "." << iMsec << " " << sMeridian << std::endl; printf("%d/%d/%d %d:%d:%d.%d %s", iMonth, iDay, iYear, iHour, iMin, iSec, iMsec, p_cMeridian);
-- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
|
TeamTerradactyl
Member #7,733
September 2006
|
You can also use the sprintf function. http://www.cplusplus.com/reference/clibrary/cstdio/sprintf.html said: This function behaves exactly as printf does, but writing its results to a string instead of stdout. I've always been more of a fan of sprintf than printf, personally. Of course, instead of the printf function, I'd suggest using textprintf_ex, since Allegro doesn't play nice with the console (unless you're outputting some debug data).
|
|
Ron Novy
Member #6,982
March 2006
|
Then what about uszprintf?
textprintf_ex is better though... ---- |
|
Tobias Dammers
Member #2,604
August 2002
|
The big strength of C++ ostreams is that you can just replace the stream without really touching the output code. For example, send a message to cout, cerr, a stringstream, a log file, even a custom 'null buffer' that just throws away the result, without changing the code inside the function that generates the message. Or you can send data from one stream to another, set up chains of filters and whatnot.
--- |
|
aj5555
Member #9,033
September 2007
|
line(screen, 100, 100, 100, 300, makecol(255,255,255) ); its a crude 1.
|
|
demiselegion
Member #9,036
September 2007
|
Thanks Harry, you did it for me. |
|
|