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

F

Francesco

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
 
V

Victor Bazarov

Francesco said:
[...]
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.

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
 
C

Chris Thompson

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...

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.
 
F

Francesco

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.


Francesco said:
[...]
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.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top