simply put, where can I find out how to put an int into a string? i.e.
int x;
string str;
str += int; //?? i dont think it works
simply put, where can I find out how to put an int into a string? i.e.
int x;
string str;
str += int; //?? i dont think it works
itoa turns your in into a char*, from there you can build a string object if you so wish
Code:/** Converts any type T to string if it has operator << (std::ostream&, const T&) defined */ template <class T> std::string toString(const T& val) { std::ostringstream strWriter; strWriter << val; return strWriter.str(); }
i hoped i wouldnt have to ask a dumb question but.. where is itoa defined? which header file? i couldnt find it...
itoa() is in conio.h, a non-standard header file for some non-standard functions like itoa(). Some compilers, like Borland have it, and others like VC++ don't.
templates and ostringstreams are standard as is sprintf(), which could also be used to convert int to string.
ony problem with templates is I currently know nothing at all about them , because I am working on Classes and strings at the moment. also I am using Linux, with gcc, so I guess i will have to go see if conipo and atoi works here, otherwise could anyone point me to some tutorials on using any of those other alternatives? It'd be greatly appreciated.
Thanx in advance.
Thanx lots guys I tried atoi but discovered either gcc don't have a conio or else i did it wrong, so i looked up sprintf() in my "C++ the complete reference (Herb Schildt)", and used it.. and it works great
thanx again,
may the source be with you
James G. Flewelling
Rgistered Linux User #327359
Athabasca University Student (BSc. CIS)
http://catb.org/~esr/faqs/smart-questions.html
http://catb.org/jargon/
http://www.ebb.org/ungeek
---GEEK CODE---
Version: 3.12
GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
----/GEEK CODE----
upd: 2005-02-11
itoa(..) is also in stdlib.h for future reference. And I have conio.h in my VC++ anyway.