# string to character conversion

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 06-06-2004
gell10
string to character conversion
Hi I was wondering how do I put an integer inside a string

For example
Code:

```<string> <iostream.h> using namespace std; main() {         int x=1;         string bob;         bob=x; ////////////I'm not sure how to convert it. i tried using                     /////////// sstream but i can't use <sstream.h>                   ////////// My compiler bugs out, I am using visual                   //////////  c+ 6.0. it reports 60 something errors in the                   ///////// header file. Is there another way to have it so                   ////////// when       cout<<bob;  ///// it will read 1? and if i have to use char how                   //////// would i do that? thanks }```
If anyone can help me thanks :-)
• 06-06-2004
Thantos
look up itoa()
• 06-06-2004
gell10
thanks that works really well, but what is a radix :-)
• 06-06-2004
Thantos
Heh I had the same question when I first used it. The radix is the base to output the string in.

So for normal decimal numbers you would use a radix of 10. For binary you'd use 2, hex 16, octo 8, trinary 3, etc :)
• 06-06-2004
gell10
thanks ok all my questions are answered... for now ;-)
• 06-06-2004
Prelude
itoa isn't a standard function. Since you're using C++ strings, the stringstream class would be a better option because it's standard and far more flexible:
Code:

```#include <iostream> #include <sstream> #include <string> using namespace std; template <typename T> string makestr(T thingie) {   ostringstream sout;   sout<< thingie;   return sout.str(); } class test {   int a, b; public:   test(int a_, int b_)     : a(a_)     , b(b_)   {}   friend ostream&   operator<<(ostream& out, const test& t)   {     return out<< t.a <<'/'<< t.b;   } }; int main() {   string s;   s = makestr(10);   cout<< s <<endl;   s = makestr(123.456);   cout<< s <<endl;   test t(4, 5);   s = makestr(t);   cout<< s <<endl; }```
• 06-06-2004
gell10
my compiler (mvc++ 6.0) cannot open <sstream> it says there are errors in that file does that mean i have to reinstall it, while itoa works just fine.
• 06-06-2004
Codeplug
I'm using VC++, works fine for me.

>> cannot open <sstream> it says there are errors in that file
How'd it know there were errors in it if it couldn't open it?

gg
• 06-06-2004
Prelude
>that would be better because no extra copy would be made, right?
No copy would be made, yes. But it would be very bad because the stringstream object is destroyed when the function returns. Returning a reference to local data is a very Bad Thing. Making the return string const also limits the function in that the caller is unable to modify the string. For such a fundamental operation, it would be unwise IMO to assume that the result would never be modified.

>2)<snip>
Because ostream is a base class for ostringstream. By using ostream you can print test using any stream that derives from ostream. If you used ostringstream then you would be limited to ostringstreams only.

>b) why when i remove the reference it doesn't compile??
Because the reference is required. ;)
• 06-06-2004
gell10
oh i mean the file opens but the compiler error reads

c:\program files\microsoft visual studio\vc98\include\sstream(22) : error C2504: 'basic_streambuf' : base class undefined
c:\program files\microsoft visual studio\vc98\include\sstream(196) : see reference to class template instantiation 'std::basic_stringbuf<_E,_Tr,_A>' being compiled

and 63 other warning statements just like it, and when i click on them it brings me to the sstream file.
• 06-06-2004
Prelude
Make sure that extensions are enabled and strict ANSI compliance is disabled. If it still doesn't compile you may need to reinstall the compiler.
• 06-06-2004
gell10
ok thanks i am going to try that
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last