Archived

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

Chryzmo

Adding an int to a string

Recommended Posts

Chryzmo    122
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
Agony    3452
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
Fidelio66    164
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
Miserable    606
quote:
Original post by oreillym
the itoa() function converts ints to strings.

The itoa() function is a non-standard function that converts ints to character arrays.

Share this post


Link to post
Share on other sites
incubator01    122
been looking for that for nearly a year :D
thanks

strange though that I cant find this header or class in the STL index at the site of SGI

[edited by - incubator01 on May 26, 2004 6:27:33 PM]

Share this post


Link to post
Share on other sites
GroZZleR    820
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!)

Share this post


Link to post
Share on other sites
SiCrane    11839
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
Miserable    606
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
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.

[edited by - DukeAtreides076 on May 26, 2004 7:58:27 PM]

Share this post


Link to post
Share on other sites
Zipster    2365
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
fooman    122
your not save with sprintf, you could print i.e. 200 64bit-integers, using full size, into a string, which has a size of 1024...

Share this post


Link to post
Share on other sites