How do I go about displaying an integer on the screen?
Thanks,
Me.
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.)
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
Beautiful work, Harry.
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);
You can also use the sprintf function.
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).
Then what about uszprintf?
textprintf_ex is better though...
gosh... I'm so bored...
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.
printf() is more intuitive, though. There's not much that keeps you from combining the two, btw:
line(screen, 100, 100, 100, 300, makecol(255,255,255) );
its a crude 1.
Thanks Harry, you did it for me.