string to character conversion

This is a discussion on string to character conversion within the C++ Programming forums, part of the General Programming Boards category; Hi I was wondering how do I put an integer inside a string For example Code: <string> <iostream.h> using namespace ...

  1. #1
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46

    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 :-)

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    look up itoa()

  3. #3
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46
    thanks that works really well, but what is a radix :-)

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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

  5. #5
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46
    thanks ok all my questions are answered... for now ;-)

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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;
    }
    My best code is written with the delete key.

  7. #7
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46
    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.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,673
    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

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  10. #10
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46
    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.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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.
    My best code is written with the delete key.

  12. #12
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46
    ok thanks i am going to try that

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >2) ok, but we don't want to modify any data mambers of the dirived class?
    Why would you want to modify an object with an output function?

    >By theway, only the ostream object "cout" is connected to the screen, right?
    C++ doesn't require a screen, cout is simply connected to the standard output. But to answer your question, yes. At program startup, the only ostream object connected to standard output is cout.

    >Huh? why?
    Because the standard says so.

    >when do these copy objects give their memory back to the system?
    When the scope in which they were created is exited:
    Code:
    object foo();
    
    int
    main()
    {
      object a = foo();
    
      for (int i = 0; i < 10; i++) {
        object b = foo();
        cout<< b <<endl;
      } // b is destroyed here
    } // a is destroted here
    My best code is written with the delete key.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >somnething gets modified in obj. Right?
    Yes. Sorry, I was thinking that you were talking about the object being written, not the stream being written to.

    >What i meant is, if some data members of "o" that do not exist in a ostream object wil get modified by this
    That won't happen. Because o is passed as a reference in operator<<, polymorphism takes effect and the correct member functions are called, modifying the correct members, even if they don't exist in the base class.

    >but why does it say so?
    See my above explanation for one reason.

    >you didn't get the question
    No, you didn't get the answer. A copy is a unique object; it's created when the copy is made and destroyed (releasing the memory that it used) when the scope in which it was created is exited. The temporary values OTOH have rules that are rather hairy. IIRC, the lifetime of a temporary object extends until the end of the containing expression. So your example might work like this:

    b is created and given the value 24 in the function main.
    An unnamed temporary initialized to b's value is made in the call to f.
    a is created and assigned the unnamed temporary value (24).
    The unnamed temporary is destroyed.
    A copy of the value of the return expression is saved as an unnamed temporary.
    a is destroyed.
    The unnamed temporary's value is assigned to b.
    The unnamed temporary is destroyed.
    b is destroyed.
    My best code is written with the delete key.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What's the data member that the ostringstream has
    I don't know. It depends heavily on internal knowledge of a specific implementation.
    My best code is written with the delete key.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21