Display a variable

This is a discussion on Display a variable within the C++ Programming forums, part of the General Programming Boards category; Hi! Does anybody know how to display a variable onto a bitmap using textout_ex(...); I've tried using char a*; int ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    21

    Display a variable

    Hi! Does anybody know how to display a variable onto a bitmap using textout_ex(...); I've tried using

    char a*;
    int b = 5;
    sprintf(a, "%i", b);
    textout_ex (buffer, font, a, 0, 0, makecol(0,0,0), makecol(255,255,255));

    This causes the program to exit with an error. Anybody know how?

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Take it to the C!

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I particularly enjoyed how you didn't even mention that textout_ex is a graphics function from Allegro.
    My best code is written with the delete key.

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Code:
    char a*;
    This creates a pointer. That is, it makes a variable that holds an address. When you create it, it has a random address.

    Code:
    sprintf(a, "%i", b);
    This starts writing information to that random address that a holds. You have no guarantee what's at that address, but you can be assured you shouldn't be writing to it.

    You need to either make a an array (which should be fine so long as you make it long enough to store the largest possible int, which should equate to an array at least 12 characters long), or you need to make the pointer point to memory you've actually allocated.
    Last edited by Cat; 11-12-2006 at 03:08 PM.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    21
    So, your saying if i make A an array, and then get the variables i want to draw to the screen into it. Then draw the array??
    Last edited by Xinco; 11-14-2006 at 01:17 PM.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    21
    Anybody know if that will work?

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    Quote Originally Posted by Cat
    Code:
    char a*;
    This creates a pointer.
    Actually this creates an error:
    c:\vart\work\my samples\hello\hello.c(6) : error C2143: syntax error : missing ';' before '*'
    c:\vart\work\my samples\hello\hello.c(6) : error C2059: syntax error : ';'
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Yes, it would work. You would format the string beforehand with sprintf(), and then pass textout_ex() and ordinary string that it can deal with. You just need to make sure that a is large enough to store whatever you assign it to.

    Did you try it?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    21
    Ok, tried it but it is displaying random characters ( ^^6r )

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    Maybe if you allocated space...
    Code:
    char a[50];
    int b = 5;
    sprintf(a, "%i", b);
    textout_ex (buffer, font, a, 0, 0, makecol(0,0,0), makecol(255,255,255));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    21
    Yep, tried it. It still only displays random characters.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    Well does it work with a fixed string?
    Did you setup the screen and font properly?

    If you can't make
    textout_ex (buffer, font, "hello world", 0, 0, makecol(0,0,0), makecol(255,255,255));
    work, then nothing else matters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    textout_ex() writes a string to a bitmap. It could be the way you're displaying the bitmap.

    Print the string directly (with printf() or fprintf() to a file) to see if it's the string or something else that is the problem.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Quote Originally Posted by vart
    Quote Originally Posted by cat
    Code:
    char a*;
    This creates a pointer.
    Actually this creates an error:
    c:\vart\work\my samples\hello\hello.c(6) : error C2143: syntax error : missing ';' before '*'
    c:\vart\work\my samples\hello\hello.c(6) : error C2059: syntax error : ';'
    shouldnt it be
    Code:
    char *a
    ?

  15. #15
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Exactly. That was vart's point.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  2. error display variable value/type
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 07-08-2008, 04:24 PM
  3. variable can auto initialize itself?
    By thinhare in forum C Programming
    Replies: 6
    Last Post: 09-13-2005, 06:07 AM
  4. Using TextOut to display variables
    By jmd15 in forum Windows Programming
    Replies: 4
    Last Post: 06-27-2005, 12:55 PM
  5. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21