I know that the atoi() function converts a string number to an int equivalent.
Is there a similar function that converts an int number to a string number.
Thanks
Mark S.
I know that the atoi() function converts a string number to an int equivalent.
Is there a similar function that converts an int number to a string number.
Thanks
Mark S.
Yep, they're called stringstreams, and they work both ways. There is also a non-standard function known as itoa(), but forget about that. Also, forget about atoi() while you're at it unless you're interested in C. The C++ way is stringstreams.Originally Posted by Mark S.
Sent from my iPadŽ
boost::lexical_cast is another choice.
The C pendant to atoi is sprintf. But for C++, stick with the streams or lexical_cast.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
>I know that the atoi() function converts a string number to an int equivalent.
That's the theory behind atoi. If you're wise you'll forget it exists. In C it has awful error handling semantics, and strtol (or even sscanf) is the recommended alternative. In C++ there are much better and safer ways to do this (such as stringstreams or the lovely boost::lexical_cast) that are consistent in both directions, unlike the C solution.
My best code is written with the delete key.
Go for string stream! I think there's an itoa() function, unless I'm making that up, but I don't think I am. stringstreams are great!
Here's a nice example that Prelude used the other day to put a number into a string :: http://cboard.cprogramming.com/showp...79&postcount=3 You can Google stringstreams, or there should be a tutorial here somewhere. This is probably helpful http://www.cplusplus.com/ref/iostream/stringstream/
>Here's a nice example that Prelude used the other day to put a number into a string
It's only slightly more complicated and vastly more useful to generalize:
Of course, that's nothing more than a simplified version of what boost::lexical_cast does.Code:#include <sstream> #include <typeinfo> namespace jsw { template <typename T, typename U> T lexical_cast ( U src ) { std::stringstream s; T dst; if ( !( s << src ) || !( s >> dst ) ) throw std::bad_cast ( "Invalid conversion" ); return dst; } }
My best code is written with the delete key.
If you compile as ANSI-compatible, then your compiler will complain that you are. It's a non-standard function, although cppreference.com has it.I think there's an itoa() function, unless I'm making that up, but I don't think I am.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.