Adding an int to a string

Started by
13 comments, last by Chryzmo 19 years, 10 months ago
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
Advertisement
the itoa() function converts ints to strings.
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]
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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);

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.
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]
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]
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!)
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.
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.

This topic is closed to new replies.

Advertisement