Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

JIMbond21

Simple question

This topic is 5458 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

How would I put an integer or any number into a string, example: int Time = 10; char[] String = "Hello dude?"; // My draw text function that takes the pointer to the char string DrawText( String /* Now how would I add the interget "Time" into the string as a number*/ ); any help is apprecieated. THX, JAP

Share this post


Link to post
Share on other sites
Advertisement
There's a few ways to do it. The two that comes off the top of my head, you can use sstream (stringstreams) or you can use the itoa() function which will turn your int into char * form.





--{You fight like a dairy farmer!}

[edited by - Greatwolf on August 10, 2003 3:00:14 AM]

Share this post


Link to post
Share on other sites
A way to do it is using itoa like...



char String[] = "Points: ";
int points = 5;

char number[20]="";
itoa(points,number,10);
strcat(String,number);
printf("%s\n",String);



[edited by - FtMonkey on August 10, 2003 3:07:06 AM]

Share this post


Link to post
Share on other sites
In C++:

#include <sstream>
#include <string>

...

int time = 10;

std::stringstream ss;
ss << "Time: " << time;
std::string str = ss.str();

DrawText( ..., str.c_str(), ... );

In C:

#include <stdio.h>

...

int time = 10;

char str[20];
sprint( str, "Time: %d", time );

DrawText( ..., str, ... );


[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on August 10, 2003 11:27:02 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!