Archived

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

Chryzmo

Adding an int to a string

Recommended Posts

Heyas, I am trying to add two ints to a string. They are the coordinates of the player''s sprite that I want to be displayed to help with debugging a problem I am helping with the collision detection in my game. Here is what I am doing now:

        // Sprite.ShowXPos() and ShowYPos() only returns the X and Y positions, which are both of type int


	string lsXPos = Sprite.ShowXPos();
	string lsYPos = Sprite.ShowYPos();
	string lsPosition = lsXPos + ", " + lsYPos;
The error I get is: e:\_Development\VC++\2dEp\game.cpp(169): error C2440: ''initializing'' : cannot convert from ''int'' to ''std::basic_string<_Elem,_Traits,_Ax>'' with [ _Elem=char, _Traits=std::char_traits, _Ax=std::allocator ] Is there some other function I need to use to be able to add the value of an int to a string? Or do I need to just find another way to do this all together? Thanks, Chris

Share this post


Link to post
Share on other sites
stringstream does the trick.

You could write a little wrapper function to do it for you:
#include <sstream>

std::string IntToString(int n)
{
std::stringstream s;
s << n;
return s.str();
}
Or you could just do this:
std::stringstream lsPosition;
lsPosition << Sprite.ShowXPos() << ", " << Sprite.ShowYPos();
std::cout << lpPosition.str();
You can use all the stuff in <iomanip> as well, such as std::setw(n) and std::setbase(n).

[edited by - Agony on May 26, 2004 5:06:06 PM]

Share this post


Link to post
Share on other sites
quote:
Here is what I am doing now:


// Sprite.ShowXPos() and ShowYPos() only returns the X and Y positions, which are both of type int


string lsXPos = Sprite.ShowXPos();
string lsYPos = Sprite.ShowYPos();
string lsPosition = lsXPos + ", " + lsYPos;




Try


// Sprite.ShowXPos() and ShowYPos() only returns the X and Y positions, which are both of type int


string lsXPos = Sprite.ShowXPos();
string lsYPos = Sprite.ShowYPos();
string lsPosition = String.Format("{0}, {1}", lsXPos, lsYPos);

Share this post


Link to post
Share on other sites
quote:
Original post by incubator01
strange though that I cant find this header or class in the STL index at the site of SGI



That would be because std::stringstream is not part of the STL.

Share this post


Link to post
Share on other sites
quote:
Original post by GroZZleR
To hell with the STL!

char szBuffer[1028];
sprintf(szBuffer, "%d, %d", int1, int2);

(Just thought I''d show that STL isn''t the only way to do this!)

However, he was using strings to begin with (not char*), and as always with C functions, you have to worry about buffer size - even with a 1028 character buffer, you really should be using snprintf(). C++ streams save you from this inconvenience.

Share this post


Link to post
Share on other sites
quote:
Original post by DukeAtreides076
He won't hit any size restrictions with a 1024 buffer and that string. ints cant hold numbers big enough to fill up 1024 spaces.

But it's possible the destination string could contain more than just integers. snprintf is the safer, still-C alternative. And why not be safe when you can be?

[edited by - Zipster on May 26, 2004 8:21:21 PM]

Share this post


Link to post
Share on other sites