convert int to const char*

This is a discussion on convert int to const char* within the C++ Programming forums, part of the General Programming Boards category; hi, i currently want to output a scoring system into a game im making through C++ and SDL the code ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    11

    convert int to const char*

    hi, i currently want to output a scoring system into a game im making through C++ and SDL the code for outputting the text onto the screen is

    SDL_Surface *text=TTF_RenderText_Blended(font, "message", col);

    the part "message" is of type const char* and i want to have an int outputted there (for example a score, or amount of ammo)
    any ideas would be vey much appreciated

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Code:
    char buff[100];
    sprintf( buff, "%d", myScore );
    SDL_Surface *text=TTF_RenderText_Blended(font, buff, col);
    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.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Code:
    #include <iostream>
    
    void foo(const char*in);
    
    int main()
    {
    	int score=5;
    	char str[2]={'\0'};
    	str[0]=static_cast<char>(score+static_cast<int>('0'));
    	foo(str);
    	return 0;
    }
    
    void foo(const char*in)
    {
    	std::cout<<in<<std::endl;
    }
    a loop and some math will help you convert more than a single digit.

    edit: or salem's method
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    11
    Quote Originally Posted by Salem
    Code:
    char buff[100];
    sprintf( buff, "%d", myScore );
    SDL_Surface *text=TTF_RenderText_Blended(font, buff, col);

    thats brilliant thanks.
    just wondering, what exactly is sprintf and that %d thing if its not too much hassle

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    sprintf is c for cout, and %d means "the first variable should be printed as a decimal"
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    11
    thats great again, thanks

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    If you're after a c++ method, then look at string streams, then extract the result using the .c_str() method.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  3. Classes & Collections
    By Max_Payne in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2007, 12:06 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

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