Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Displaying ints

Credits go to StevenVI for helping out!
This thread is locked; no one can reply to it. rss feed Print
Displaying ints
demiselegion
Member #9,036
September 2007

How do I go about displaying an integer on the screen?

Thanks,
Me.

StevenVI
Member #562
July 2000
avatar

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.)

__________________________________________________
Skoobalon Software
[ Lander! v2.5 ] [ Zonic the Hog v1.1 ] [ Raid 2 v1.0 ]

Ron Novy
Member #6,982
March 2006
avatar

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 ;)

----
Oh... Bieber! I thought everyone was chanting Beaver... Now it doesn't make any sense at all. :-/

Mark Oates
Member #1,146
March 2001
avatar

Beautiful work, Harry. :-*

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

bamccaig
Member #7,536
July 2006
avatar

Harry Carey said:

If you are using C++, you also can use the std::cout object, though to me it is less intuitive...

Fixed. :D

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);

TeamTerradactyl
Member #7,733
September 2006
avatar

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.

/* sprintf example */
#include <stdio.h>

int main()
{
  char buffer[50];
  int n, a = 5, b = 3;
  n = sprintf(buffer, "%d plus %d is %d", a, b, a + b);
  printf("[%s] is a %d char long string\n", buffer, n); // To output to console
  return 0;
}

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
avatar

Then what about uszprintf?

 char buffer[10];
 int player_score;
 ...
 uszprintf(buffer, sizeof(buffer), "Your score is: %d", player_score);

 /* Show the string in blue letters near the bottom of the screen. */
 textout_ex(screen, font, buffer, 10, SCREEN_H - 10, makecol(0, 0, 255), -1);

See Also:

textprintf_ex is better though...
gosh... I'm so bored...

----
Oh... Bieber! I thought everyone was chanting Beaver... Now it doesn't make any sense at all. :-/

Tobias Dammers
Member #2,604
August 2002
avatar

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:

cout << sprintf(mybuf, "Printing a random number: %i", rand()) << endl;

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

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.

Go to: