float to string object in c++, help and discussion...

Discussion in 'C++' started by Francesco, Jul 29, 2003.

  1. Francesco

    Francesco Guest

    Ciao,
    I'm new in c++ programming and I've experimented some method to convert
    real number into c++ string object, at present I've found as best solution
    use the C command "sprintf":
    //------------------------------------------------------
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    using namespace std;

    //-- Corpo del programma
    int main(int argc, char *argv[])
    {
    char buff[30];
    string risultato;

    sprintf(buff,"%3.15f", 1.572757843657862662);
    risultato = (string) buff;

    cout << risultato << endl;
    system("PAUSE");
    return 0;
    }
    //------------------------------------------------------
    Output:
    1.572757843657863
    Premere un tasto per continuare...
    //------------------------------------------------------
    I've tried another algorithm with "pure" c++ code...

    //------------------------------------------------------
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    #include <sstream>

    using namespace std;

    int main(int argc, char *argv[])
    {
    stringstream buff;
    string risultato;
    buff << 12.34324435;
    buff >> risultato;
    cout << risultato << endl;

    system("PAUSE");
    return 0;
    }
    //-------------------------------------------------
    Output:
    12.3432
    Premere un tasto per continuare...
    -----------------------------------------------------
    but I lose several decimal digits....

    By the way, several people say it's not good C++ programming to use C code
    (for example the Stroustrup's book), but I think it's useful to use
    sprintf!!
    May somebody tell me if exist a C++ function that works like sprintf and
    I've missed???

    What do you think about to use C code inside C++???

    Thanks,
    Checco
     
    Francesco, Jul 29, 2003
    #1
    1. Advertising

  2. "Francesco" <> wrote...
    > [...]
    > sprintf(buff,"%3.15f", 1.572757843657862662);
    >[...]
    > stringstream buff;
    > string risultato;
    > buff << 12.34324435;
    > buff >> risultato;


    These two are definitely not the same. Try losing those numbers in
    the format string in 'sprintf' (use "%f" instead of "%3.15f") and
    see what happens.

    > -----------------------------------------------------
    > but I lose several decimal digits....


    You don't lose anything. Instead, with 'sprintf' you _specifically_
    _gain_ more digits by using the precision (that "15" after the dot).

    > By the way, several people say it's not good C++ programming to use C code
    > (for example the Stroustrup's book), but I think it's useful to use
    > sprintf!!


    Nobody is going to tell you what to think. Think whatever you please.

    > May somebody tell me if exist a C++ function that works like sprintf and
    > I've missed???


    No, there is no "C++ function that works like sprintf". However, if
    you want to control how many digits are output to 'cout', see the
    description of "I/O manipulators" in your favourite book. Learn about
    the available functionality instead of substituting it with something
    you already know. Key words here are "setprecision", "setw", "fixed".

    > What do you think about to use C code inside C++???


    I think it is unnecessary.

    Victor
     
    Victor Bazarov, Jul 29, 2003
    #2
    1. Advertising

  3. On Tue, 29 Jul 2003 18:40:08 +0000, Francesco wrote:

    >
    > Ciao,
    > I'm new in c++ programming and I've experimented some method to convert
    > real number into c++ string object, at present I've found as best solution
    > use the C command "sprintf":
    > //------------------------------------------------------
    > #include <iostream>
    > #include <stdlib.h>
    > #include <string>
    > using namespace std;
    >
    > //-- Corpo del programma
    > int main(int argc, char *argv[])
    > {
    > char buff[30];
    > string risultato;
    >
    > sprintf(buff,"%3.15f", 1.572757843657862662);
    > risultato = (string) buff;
    >
    > cout << risultato << endl;
    > system("PAUSE");
    > return 0;
    > }
    > //------------------------------------------------------
    > Output:
    > 1.572757843657863
    > Premere un tasto per continuare...
    > //------------------------------------------------------
    > I've tried another algorithm with "pure" c++ code...
    >
    > //------------------------------------------------------
    > #include <iostream>
    > #include <stdlib.h>
    > #include <string>
    > #include <sstream>
    >
    > using namespace std;
    >
    > int main(int argc, char *argv[])
    > {
    > stringstream buff;
    > string risultato;
    > buff << 12.34324435;
    > buff >> risultato;
    > cout << risultato << endl;
    >
    > system("PAUSE");
    > return 0;
    > }
    > //-------------------------------------------------
    > Output:
    > 12.3432
    > Premere un tasto per continuare...
    > -----------------------------------------------------
    > but I lose several decimal digits....


    The standard way of doing this is as follows:

    template <typename T>
    std::string toString(const T &thing) {
    std::eek:stringstream os;
    os << thing;
    return os.str();
    }

    This will convert any data type capable of being output into a string.
     
    Chris Thompson, Jul 29, 2003
    #3
  4. Francesco

    Francesco Guest

    Thanks Victor!!!
    It works perfectly... and I've learned a lot about cout (I'm at the begin
    of the book :)
    the manipulator are at pag.726 ;) ;)

    Thanks to Chris, too
    Evenif it was not a solution for my problem (I'm sorry but my english is
    terrible and it's very easy to misunderstand me) I've learned from you
    about template.

    Greetings from Italy ;)
    Checco.


    On Tue, 29 Jul 2003 14:53:35 -0400, Victor Bazarov <>
    wrote:

    > "Francesco" <> wrote...
    >> [...]
    >> sprintf(buff,"%3.15f", 1.572757843657862662);
    >> [...]
    >> stringstream buff;
    >> string risultato;
    >> buff << 12.34324435;
    >> buff >> risultato;

    >
    > These two are definitely not the same. Try losing those numbers in
    > the format string in 'sprintf' (use "%f" instead of "%3.15f") and
    > see what happens.
    >
    >> -----------------------------------------------------
    >> but I lose several decimal digits....

    >
    > You don't lose anything. Instead, with 'sprintf' you _specifically_
    > _gain_ more digits by using the precision (that "15" after the dot).
    >
    >> By the way, several people say it's not good C++ programming to use C
    >> code
    >> (for example the Stroustrup's book), but I think it's useful to use
    >> sprintf!!

    >
    > Nobody is going to tell you what to think. Think whatever you please.
    >
    >> May somebody tell me if exist a C++ function that works like sprintf and
    >> I've missed???

    >
    > No, there is no "C++ function that works like sprintf". However, if
    > you want to control how many digits are output to 'cout', see the
    > description of "I/O manipulators" in your favourite book. Learn about
    > the available functionality instead of substituting it with something
    > you already know. Key words here are "setprecision", "setw", "fixed".
    >
    >> What do you think about to use C code inside C++???

    >
    > I think it is unnecessary.
    >
    > Victor
    >
    >
    >
     
    Francesco, Jul 29, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bd
    Replies:
    0
    Views:
    660
  2. Alexander Malkis
    Replies:
    10
    Views:
    921
  3. float ** and float *p[]

    , Dec 6, 2006, in forum: C Programming
    Replies:
    8
    Views:
    368
    Barry Schwarz
    Dec 7, 2006
  4. Florian Lindner
    Replies:
    1
    Views:
    660
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,628
    James Kanze
    Oct 8, 2009
Loading...

Share This Page