• Advertisement

Archived

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

Adding an int to a string

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

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
Advertisement
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 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
You can't concatenate an int to a string like that. Look at streams.

edit: already answered I guess...

[edited by - DukeAtreides076 on May 26, 2004 5:07:04 PM]

Share this post


Link to post
Share on other sites
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
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
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
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
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
If I wanted to be safe, I''d use the STL in the first place.

I live on the edge baby! ^_^

Share this post


Link to post
Share on other sites
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

  • Advertisement