1 Replies - 804 Views - Last Post: 10 December 2012 - 11:46 PM Rate Topic: -----

#1 Pin Head  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 228
  • Joined: 31-July 09

Insert problem

Posted 10 December 2012 - 09:09 PM

Hi
Im having trouble with this line of code what I am trying to do is add the counter to # so it works like this

#1, #2, #3 but for some reason the int isn't added to the end?

num.insert( counter, "#" + counter );



void CubeData::findXYZPoints1()
{

	string num = " -2.996580 -1.823648 2.654824";

	unsigned int counter = 0;
	
        cout << "Size of String: " << num.length() << endl;

	while( counter < num.length() )
	{
		if( ' ' == num.at( counter ) )
		{
			num.erase( counter, 1 );
			num.insert( counter, "#" + counter );
			counter++;			
		}
		counter++;
	}
	cout << "Size After insert: " << num.length() << endl;
	cout << "Replace:" << num << endl;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Insert problem

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,451
  • Joined: 09-June 09

Re: Insert problem

Posted 10 December 2012 - 11:46 PM

"#" is a string constant, adding a numerical value to it is equivalent of pointer arithmetic (not what you are looking for). You can concatenate characters to integers using a string stream.

i.e.
#include <iostream>
#include <sstream>

using namespace std;
int main(int argc, char *argv[]) {
   
   std::stringstream ss;
   ss<<"#";
   ss<<2;
   std::cout<<ss.str()<<std::endl;
   
   return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1