By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,331 Members | 1,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,331 IT Pros & Developers. It's quick & easy.

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

P: n/a

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
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Francesco" <ch***********@tin.it> 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
Jul 19 '05 #2

P: n/a
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::ostringstream os;
os << thing;
return os.str();
}

This will convert any data type capable of being output into a string.

Jul 19 '05 #3

P: n/a
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 <v.********@attAbi.com>
wrote:
"Francesco" <ch***********@tin.it> 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

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.