Thread: Probably simple question: Displaying int's

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    25

    Probably simple question: Displaying int's

    I'm using DrawText to write some text to my screen. I need to also show some integers, but that function only takes in Strings.

    So I guess my question is either one of the two:
    1) how do I convert an int to a string?
    or
    2) is there some better function to use to show text in windows? A function that can also show ints...

    Thank you!

    Nimit

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: Probably simple question: Displaying int's

    Originally posted by conright
    I'm using DrawText to write some text to my screen. I need to also show some integers, but that function only takes in Strings.

    So I guess my question is either one of the two:
    1) how do I convert an int to a string?
    or
    2) is there some better function to use to show text in windows? A function that can also show ints...

    Thank you!

    Nimit
    You'll need to convert the integer into a string. Here is a quick example. The function you need is sprintf

    Code:
    char buffer[256] = { 0 };
    int nNumber = 123;
    
    sprintf( buffer, "The number is %d", nNumber );
    Then you will use TextOut or DrawText to display buffer and it will display "The number is 123". Hope this helps.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    20
    int i;
    char a[10];

    TextOut(hdc, x, y, a, wsprintf(a, "%d", i));

  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

  5. #5
    someone told me you shouldn't use itoa, can someone explain to me why?

  6. #6
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    you can use it like this
    Code:
    itoa(x,chBuffer,10);
    x is an int chBuffer is a char array and 10 is the base

    it works fine for me, i use it a few times in a prog and have not had any trouble with it
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  7. #7
    I used a bunch in a text-based game I made, and never had a problem. I was just wondering, since we are kinda on the topic.

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Originally posted by frenchfry164
    someone told me you shouldn't use itoa, can someone explain to me why?

    itoa is not standard, so you aren't always going to have access to it, decreasing the portablility of you code. Use sprintf or a stringstream (or write your own function)

    -Futura
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    35
    For the record -- use wsprintf if programming for Windows, which I assume you are if you're having problems printing ints.

    Using wsprintf and TextOut correctly looks like this:

    Code:
    int score = 0;
    char chScore[] = "Score: %d";
    char scoreString[20];
    
    wsprintf(scoreString, chScore, score);
    
    TextOut(hdc, x, y, scoreString, strlen(scoreString));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM