int to string - found method not working

This is a discussion on int to string - found method not working within the C++ Programming forums, part of the General Programming Boards category; Hi, I tried the following code to convert an int to a string: Code: int i = 5678; string s; ...

  1. #1
    epb
    epb is offline
    Registered User
    Join Date
    Oct 2009
    Posts
    5

    int to string - found method not working

    Hi,

    I tried the following code to convert an int to a string:

    Code:
    int i = 5678;
    string s;
    stringstream out;
    out << i;
    s = out.str();
    But it returns an empty string! If I print the "out" variable using
    Code:
    cout << out << endl;
    It prints "0". What could possibly be wrong? I'm using Mac OS X 10.6.1 and gcc version 4.2.1.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Try printing s instead of out and you will probably find that out.str() did not return an empty string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    Portugal
    Posts
    25
    you can do lyke this

    int val=2635;
    char s[50];
    cout<<itoa(val,s,10)<<endl;

    is that you want?

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    Quote Originally Posted by joseCarlos View Post
    you can do lyke this

    int val=2635;
    char s[50];
    cout<<itoa(val,s,10)<<endl;

    is that you want?
    Except that's C and not defined in ANSI-C nor a part of C++?

  5. #5
    epb
    epb is offline
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    Quote Originally Posted by laserlight View Post
    Try printing s instead of out and you will probably find that out.str() did not return an empty string.
    Sorry if I wasn't clear about this.. I have already tried printing out 's', and that revealed an empty string (which is why I concluded that the code returns an empty string).

    Quote Originally Posted by joseCarlos
    you can do lyke this

    Code:
    int val=2635;
    char s[50];
    cout<<itoa(val,s,10)<<endl;
    is that you want?
    No that is not what I want.. I want to convert the int directly to a C++ string (of the string class)

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by epb
    Sorry if I wasn't clear about this.. I have already tried printing out 's', and that revealed an empty string (which is why I concluded that the code returns an empty string).
    Okay, so given this program:
    Code:
    #include <string>
    #include <sstream>
    #include <iostream>
    
    int main()
    {
        using namespace std;
        int i = 5678;
        string s;
        stringstream out;
        out << i;
        s = out.str();
        cout << s << endl;
    }
    Compile and run it. What output do you get?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    epb
    epb is offline
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    Quote Originally Posted by laserlight View Post
    Okay, so given this program:
    Code:
    #include <string>
    #include <sstream>
    #include <iostream>
    
    int main()
    {
        using namespace std;
        int i = 5678;
        string s;
        stringstream out;
        out << i;
        s = out.str();
        cout << s << endl;
    }
    Compile and run it. What output do you get?
    I get no output. If I add
    Code:
    cout << out << endl;
    after the assignment of 'i' to 'out' I get '0' as a single output.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by epb View Post
    I get no output. If I add
    Code:
    cout << out << endl;
    after the assignment of 'i' to 'out' I get '0' as a single output.

    Check this out

    Code:
    #include <string>
    #include <sstream>
    #include <iostream>
    
    int main()
    {
      using namespace std;
      int i = 5678;
      string s;
      stringstream out;
      out << i;
      cout << out.str() << endl;
      s = out.str();
      cout << s << endl;
    }

  9. #9
    epb
    epb is offline
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    Quote Originally Posted by RockyMarrone View Post
    Check this out

    Code:
    #include <string>
    #include <sstream>
    #include <iostream>
    
    int main()
    {
      using namespace std;
      int i = 5678;
      string s;
      stringstream out;
      out << i;
      cout << out.str() << endl;
      s = out.str();
      cout << s << endl;
    }
    This is also gives me an empty output

    EDIT: The output is two empty lines, so I guess you can say that the output is not completely empty
    Last edited by epb; 10-26-2009 at 08:28 AM.

  10. #10
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by epb View Post
    This is also gives me an empty output

    EDIT: This gives me two empty lines, so I guess you can say that the output is not entirely empty
    I don't know why you are not getting the output but see my shell here

    and for your information i m on linux 2.6.29.4 and gcc 4.3.2

    [rocky@localhost test]$ ls
    a.out test.cc
    [rocky@localhost test]$ cat test.cc
    Code:
    #include <string>
    #include <sstream>
    #include <iostream>
    
    int main()
    {
      using namespace std;
      int i = 5678;
      string s;
      stringstream out;
      out << i;
      cout << out.str() << endl;
      s = out.str();
      cout << s << endl;
    }
    [rocky@localhost test]$ g++ test.cc
    .[rocky@localhost test]$ ./a.out
    5678
    5678

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    According to this thread:

    Cocoabuilder - (Dmitry Markman) ostringstream problem with Xcode 3.2

    Assuming you're using Xcode, try setting it to build as Release rather than Debug?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by epb
    after the assignment of 'i' to 'out' I get '0' as a single output.
    hmm... that should imply that the stringstream is in a failed state, which is strange.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    epb
    epb is offline
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    Quote Originally Posted by JacobN View Post
    According to this thread:

    Cocoabuilder - (Dmitry Markman) ostringstream problem with Xcode 3.2

    Assuming you're using Xcode, try setting it to build as Release rather than Debug?
    Yes I am using Xcode. And that worked! Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 11-30-2007, 02:51 AM
  2. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 08:59 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM

Tags for this Thread


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