Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting from Int to String

Status
Not open for further replies.

crrrazydan

Programmer
Jul 27, 2000
34
US
I am making a game with OpenGL, and I have a function that prints the fps (frames per second) on the screen. But the problem is that that function only takes strings in the char *str form, and my fps value is an Int. I am a beginner to C++ (not to 3D though :), I'm used to VB), so please explain any suggestions you give a little. Here is my code:


char *str;
int fps;

GetFps(fps);
//Something to convert int to *str here
PrintString(listBase, str);


And for future reference, how do I combine two strings in C++? Like str = str + str2

Thanks! -Dan K
 
1 question: try with printf family

GetFps(fps);
printf(str,"%d",fps);
PrintString(listBase, str);

don't forget to allocate memor for str.


2 question : if u mean String the class string then u can
use str.append(str2); or str += str2;
 
This example uses the fancy stringstream class in C++

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

string myString = convertToString(123);

std::string convertToString(int x)
{
std::eek:stringstream o;
if (o << x)
return o.str();
// some sort of error handling goes here...
return &quot;conversion error&quot;;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top