Computer Science Canada convet int to string? |
Author: | hamid14 [ Sun Nov 21, 2010 1:24 pm ] |
Post subject: | convet int to string? |
I want to convert day which is an int type, to displayDays, which is a string type variable. im using dev-c++, any help is appreciated. |
Author: | TheGuardian001 [ Sun Nov 21, 2010 2:52 pm ] |
Post subject: | Re: convet int to string? |
the itoa()method will convert a int to a string (or rather, a char array). Likewise, atoi will convert a string to an int. |
Author: | OneOffDriveByPoster [ Sun Nov 21, 2010 4:50 pm ] |
Post subject: | Re: convet int to string? |
itoa is not portable. You can use sprintf or an ostringstream, |
Author: | hamid14 [ Sun Nov 21, 2010 5:10 pm ] |
Post subject: | Re: convet int to string? |
Any examples? I cant seem to find any good ones online >.> |
Author: | OneOffDriveByPoster [ Mon Nov 22, 2010 8:12 pm ] |
Post subject: | Re: convet int to string? |
hamid14 @ Sun Nov 21, 2010 5:10 pm wrote: Any examples? I cant seem to find any good ones online >.> A useful skill in programming is to be able to build solutions from documentation of APIs and the such. For example, the declaration for std::ostringstream can be added into your program by including <sstream>. std::ostringstream is a typedef for a specialization of std::basic_ostringstream (which has a member function called str()). So you can create an ostringstream object, use an inserter on it with the number to output and then get a string back with the output. |