Page 1 of 2 12 LastLast
Results 1 to 40 of 44

Thread: convert double to string (string.h string)

  1. #1

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197

    convert double to string (string.h string)

    Im having a bit of trouble with conversions to this string thing (from string.h). Is there an all knowing conversion table or something of the like?

    PHP Code:
    string s;
    double d;
    3.14
    string(d);
    cout << << endl
    there isnt a constructor with a double argument...
    retired member. Thanks for everything

  2. #2
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157
    Hi,
    I just got this really cool summary of converting numbers to strings and vice versa from a German forum http://www.c-plusplus.de...submitted by the C++ moderator. Look beyond the German and you can read the code.

    Code:
    a) Zahl nach String: (numbers to string)
    
    1. std::ostringstream
    
    
    #include <sstream>
    #include <string>
    #include <iostream>
    using namespace std;
    int main()
    {
        double d = 3.14;
        ostringstream Str;
        Str << d;
        string ZahlAlsString(Str.str());
        cout << ZahlAlsString << endl;
    }
    
    2. std::ostrstream
    
    #include <strstream>
    #include <string>
    #include <iostream>
    using namespace std;
    int main()
    {
        double d = 3.14;
        char buffer[10];
        ostrstream Str(buffer, 10);
        Str << d << ends;
        string ZahlAlsString(Str.str());
        cout << ZahlAlsString << endl;
    }
    
    3. sprintf
    
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
        double d = 3.14;
        char buffer[10];
        sprintf(buffer, "%g", d);
        printf("%s\n", buffer);
        system("pause");
    }
    
    
    b) String nach Zahl (string to number)
    
    1. std::stringstream
    
    #include <sstream>
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main()
    {   
        string AlsString("3.14");
        stringstream Str;
        Str << AlsString;
        double d;
        Str >> d;
        cout << d << endl;
    }
    2. std::strstream
    
    
    #include <strstream>
    #include <string>
    #include <iostream>
    using namespace std;
    int main()
    {   
        string AlsString("3.14");
        char buffer[10];
        strstream Str(buffer, 10);
        Str << AlsString << ends;
        double d;
        Str >> d;
        cout << d << endl;
    }
    3. atof, atoi, atol
    
    
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    int main()
    {   
        const char* AlsString = "3.14";
        double d = atof(AlsString);
        cout << d << endl;
    }
    4. sscanf
    
    
    #include <cstdio>
    #include <iostream>
    using namespace std;
    int main()
    {   
        const char* AlsString = "3.14";
        float d = 0;
        // ACHTUNG! sscanf erwartet ein float. Kein double!
        sscanf(AlsString, "%g", &d);
        cout << d << endl;
    }
    5. strtod, strtol, strtoul
    
    
    #include <cstdlib>
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {   
        string AlsString("3.145");
        char* StopPosition = 0;
        double d = strtod(AlsString.c_str(), &StopPosition);
        cout << d << endl;
    }
    Regards,
    ChuckB

  3. #3
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    string streams are pretty nice =)...
    Code:
    strstream sstream;
    sstream << "10" << endl;
    sstream << 20 << endl;
    string s;
    long l;
    sstream >> l
    sstream >> s
    cout << sstream.str() << endl << endl;
    cout << l << endl;
    cout << s << endl;
    Z.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Zaei - use stringstreams, not strstreams

    Oh, quick function for you:
    Code:
    template <typename T>
    string toString(const T &ref) {
        ostringstream oss;
        oss << ref;
        return oss.str();
    }
    ...or something
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Forgive me =). It was late =).

    Z.

  6. #6

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    Perfect everyone

    Templates work in g++? Ill assume yes; otherwise reply
    retired member. Thanks for everything

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Well, if you use the 3-series, then yes.

    3.2 is the latest I think...but it can't compile the kernel. Oops. Need 2.95.3 for that
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    then *** is it a new version if it cant do that?
    retired member. Thanks for everything

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    There may be some dropped extensions that the kernel used, this would explain why it doesn't work anymore.

    Another explanation would be that it's simply a bug.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Most people point at the second. Linus recommends 2.95.3, and I haven't been able to compile the 2.5.38 kernel with anything else!

    For most normal code, I use 3.2, because it's C++ support is far better.

    2.95 can do templates, but it doesn't support the more esoteric features.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Why do you need the kernel recompiled?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The 2.5 series is the current development version.

    /me is just playing

    I built 2.4.19 on my other disk as part of a test (trying to put something minimalist together).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Out of curiosity, is there any Linux IDE/Code Editor running around out there that has vim style code indentation?

    Example:
    Code:
    int main()
    {
    >>>>code goes over here when I hit enter
    >>>>same thing
    } <<<< curley brace comes back over here, without me hitting that backspace key
    vim, and MSVC are the only editors that I have seen that do this...

    Z.

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    nedit and emacs can do it.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  15. #15
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Figured emacs could do it =).

    Z.

  16. #16
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It does everything.

    If you've got it, at the console type
    Code:
    $ emacs -batch -l dunnet
    Enjoy!

    (found that one at work...my productivity dropped for a couple of weeks )
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  17. #17

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    Ok Im having a bit of trouble using the template. I cant make a prototype that works. They all stem massive ammounts of errors.
    retired member. Thanks for everything

  18. #18
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code, and errors?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  19. #19
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Edit++ in Windows can do it too...
    But you probably won't get it running, not even under WINE, as it uses MFC and all kinds of weird windows features such as OLE drag/drop, embedded IE etc.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  20. #20
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    And while we're at gcc...

    Anyone know where I can get a x86 binary of gcc 3.x or any C compiler that can compile gcc 3.2 for RedHat? I have a RedHat-derived server distro called e-smith (now SME server), and it doesn't come with any C compiler. Only as and ld (so if you got assembler source for any compiler it's ok too ).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  21. #21
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Can't you just install the 2.95.3 binary RPM? That'll let you build 3.2.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  22. #22
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Hmm, gotta search again, last time I looked I was sure I didn't find any RPM for gcc.

    I'll look for it tomorrow.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  23. #23
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    There must be one, it needs to come with RedHat
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  24. #24
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yet that doesn't mean it's available in public.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  25. #25
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I don't follow. It must be available, almost by definition.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  26. #26
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Originally posted by parksie
    It does everything.

    If you've got it, at the console type
    Code:
    $ emacs -batch -l dunnet
    Enjoy!

    (found that one at work...my productivity dropped for a couple of weeks )
    ROFL =). Thats pretty good =). Now, where is the emacs fridge? =).

    Z.

  27. #27
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It's quite a good game if you recognise the jokes, and if you've been hacking a while

    I missed a fair few, but got enough to redeem myself

    There's everything in there I'm busy trying to learn how to use it. Managed to create two files, get them in separate windows (Emacs windows, not X windows -- all terminal! w00t!) and compile, viewing the output

    And all this running off cryptic keyboard shortcuts and no mouse...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  28. #28
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    I feel a whole lot more comfortable in vim, but emacs seems a LOT more powerful... If im doing some coding, ill open two telnet sessions... leave one in vim, and use the other for compiling.. saves a lot of back and forth.

    And who could think of NOT doing it without the mouse... I mean... what is that? =).

    Z.

  29. #29
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    A lot more powerful? It has everything bar the kitchen sink. No, wait, that's in there

    It even has keyboard shortcuts for moving around so you don't have to move from the touch-typing position to the arrow keys Quality stuff
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  30. #30
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Its like an operating system... =).

    Z.

  31. #31
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Pretty much, yeah, only it has more features You can do most of what you'd often do from inside it, mail, news, programming.

    Mostly everything except browse the web (although who knows, maybe...)
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  32. #32
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    lynx.. im sure you can use lynx from it... =).

    Z.

  33. #33
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    /me tries if he can remember those damn key combos
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  34. #34

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    Originally posted by parksie
    Zaei - use stringstreams, not strstreams

    Oh, quick function for you:
    Code:
    template <typename T>
    string toString(const T &ref) {
        ostringstream oss;
        oss << ref;
        return oss.str();
    }
    ...or something
    that.
    retired member. Thanks for everything

  35. #35
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You what?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  36. #36
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    emacs is a religion.

    http://www.gnu.org/fun/humor.html
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  37. #37

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    This is the code that doesnt work without a prototype. What prototype should be used for this?
    PHP Code:
    template <typename T>
    string toString(const &ref) {
        
    ostringstream oss;
        
    oss << ref;
        return 
    oss.str();

    retired member. Thanks for everything

  38. #38
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    template <typename T>
    std::string toString(const T& ref);


    BTW I would inline this one.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  39. #39

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197

    not working

    PHP Code:
    #include <iostream>
    #include <string>
    using namespace std;

    template <typename T>
    std::string toString(&ref);

    int main()
     {
        
    double d 3.14;
        
    string s;
        
    toString(d);
        
    cout << << endl;

    }

    template <typename T>
    std::string toString(&ref)
     {
        
    ostringstream oss;
        
    oss << ref
        
    return oss.str();
     } 
    g++ -Wall test.cpp yields:
    test.cpp: In function `int main ()':
    test.cpp:10: universal-character-name `\u00a0' not valid in identifier
    test.cpp:10: universal-character-name `\u00a0' not valid in identifier
    test.cpp:10: sorry, not implemented: universal characters in
    identifiers
    test.cpp:10: universal-character-name `\u00a0' not valid in identifier
    test.cpp:10: sorry, not implemented: universal characters in
    identifiers
    test.cpp:10: universal-character-name `\u00a0' not valid in identifier
    test.cpp:10: sorry, not implemented: universal characters in
    identifiers
    test.cpp:10: `___double' undeclared (first use this function)
    test.cpp:10: (Each undeclared identifier is reported only once for each
    function it appears in.)
    test.cpp:10: parse error before `='
    test.cpp:12: `d' undeclared (first use this function)
    test.cpp: In function `string toString (T &)':
    test.cpp:20: universal-character-name `\u00a0' not valid in identifier
    test.cpp:20: universal-character-name `\u00a0' not valid in identifier
    test.cpp:20: sorry, not implemented: universal characters in
    identifiers
    test.cpp:20: universal-character-name `\u00a0' not valid in identifier
    test.cpp:20: sorry, not implemented: universal characters in
    identifiers
    test.cpp:20: universal-character-name `\u00a0' not valid in identifier
    test.cpp:20: sorry, not implemented: universal characters in
    identifiers
    test.cpp:20: parse error before `;'
    test.cpp:22: parse error before `return'
    retired member. Thanks for everything

  40. #40
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I think something messed up when you typed it in. It's complaining at the tokenising level that you've got some bad characters in there.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width