Jump to content
  • Advertisement
Sign in to follow this  
vrihai

How do I put numbers into a string?

This topic is 5124 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to display the FPS in my program and I'm passing a char* to my text blitter function, but I can't find any help documentation on putting the int values in a string/char.

Share this post


Link to post
Share on other sites
Advertisement
In C, you can use sprintf or (better) snprintf.
A preferable C++ alternative would be using a stringstream (just like any ostream), obtaining the resulting string, and using the c_str( ) member to get a char* member.

Although in that case, you will need to be passing a const char * to your blitter function (or create a copy from it by hand before passing it).

Share this post


Link to post
Share on other sites
Quote:
Original post by Jimmy Valavanis
or use the itoa() function.


Unless, of course, you are programming for a platform which doesn't have that non-standard function.

Share this post


Link to post
Share on other sites
Example of string streams:


#include <sstream>
#include <iostream>
using namespace std;
int SomeInt = 8534895;
char *Str = "Some String";

//join the two together
stringstream StrStream;

StrStream << Str << " " << SomeInt;

//output the string as a const char*
cout << StrStream.str().c_str();


The above snippet will display "Some String 8534895"

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!