Archived

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

JIMbond21

Simple question

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
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