5 Replies - 551 Views - Last Post: 03 April 2013 - 11:09 AM Rate Topic: -----

#1 billh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 13-April 07

portable itoa in c++

Posted 03 April 2013 - 04:22 AM

Sorry if this is a boring question but I have just decided to try c++ after learning c many years ago. Does anyone know if there is a SIMPLE to understand portable alternative to the ITOA function? I would be grateful for any replies. Thanks in advance.
Is This A Good Question/Topic? 0
  • +

Replies To: portable itoa in c++

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,688
  • Joined: 05-May 12

Re: portable itoa in c++

Posted 03 April 2013 - 05:43 AM

What do you mean by portable? Since you are using C++, you could just use the stringstream class.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 3992
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: portable itoa in c++

Posted 03 April 2013 - 06:31 AM

Please show some code that illustrates your problem. The itoa() function is portable, since it is a standard C function.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 billh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 13-April 07

Re: portable itoa in c++

Posted 03 April 2013 - 10:49 AM

Thanks Jim

This is a sample of my code:
It is to be used to display, in a window, the width and heaight of that window. It works with ITOA but I keep seeing on websites that this is not a standard c++ function

//next displays size of window
wdth=getmaxx();//actual window size
ht=getmaxy();//actual window size
startx=wdth/2;
starty=ht/2;

itoa(wdth,buffer,10);//converts i nteger to string
itoa(ht,bufferb,10);//converts integer to string
moveto(100,20);
outtext("width= ");
outtext(buffer);
moveto(100,40);
outtext("height=");
outtext(bufferb);
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2891
  • View blog
  • Posts: 10,025
  • Joined: 08-August 08

Re: portable itoa in c++

Posted 03 April 2013 - 11:02 AM

itoa converts to a C string. If you want to convert to a C++ string you could do this:
string IntToStr(int n)
{
	std::ostringstream result;
	result << n;
	return result.str();
}


Was This Post Helpful? 0
  • +
  • -

#6 billh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 13-April 07

Re: portable itoa in c++

Posted 03 April 2013 - 11:09 AM

Thanks as well skydiver
I have now looked up stringstream and have tried and succeeded in using it for a test

View PostCTphpnwb, on 03 April 2013 - 11:02 AM, said:

itoa converts to a C string. If you want to convert to a C++ string you could do this:
string IntToStr(int n)
{
	std::ostringstream result;
	result << n;
	return result.str();
}


Thanks I have now found out how to use stringstream. Thanks for all your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1