Please help me out in this problem..
I need to use scietific notation (1.2e-2 like) for any given number..
i used cout.flags(ios_base::scientific)
now I see the output in 'e' format which is OK...
now I want to capture this output in a string..so basically I want to
capture cout output in a char * buffer ...
How should I do that...
One long way is to write the number in file and then read the file in
a char buffer..
Is their any smarter way for this ?
std::stringstream strm;
double gh = 12354.6786545345436;
strm<< scientific <<gh;
std::cout <<strm.str();
> now I see the output in 'e' format which is OK...
> now I want to capture this output in a string..so basically I want to
> capture cout output in a char * buffer ...
> How should I do that...
>
The <sstream> defines streams operating on strings not real files.
Essentially, you can put strings, numbers, formatted or unformatted into
such a stream and use/convert it into another form. All works the same
as cout, cin, ofstream, ifstream but you use strings instead of real
files or standard input/output. im gonna provide a part of my file here
so u can familiar with manipulators and stringstreams
// code
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
int main(int argc, char *argv[])
{
double tabd[] = {1.23456, 0.000178, 1654.8997, 10};
const size_t roz = sizeof(tabd)/sizeof(double);
cout << "Standard output:" << endl;
for(int i=0; i<roz; ++i) {
cout << "\t" << tabd[i] << endl;
}
cout << endl;
cout << "Scientific notation with lowercase e" << endl;
for(int i=0; i<roz; ++i) {
cout << scientific << setprecision(2) << "\t" << tabd[i] << endl;
}
cout << endl;
cout << "Scientific notation with uppercase E" << endl;
for(int i=0; i<roz; ++i) {
cout << scientific << setprecision(2) << uppercase << "\t" << tabd[i]
<< endl;
}
cout << endl;
ostringstream oss; // string stream from <sstream>
oss << scientific << setprecision(2) << uppercase << tabd[1];
cout << "iss stream contains: " << oss.str() << endl; // str() returns
anything in iss in string form
cout << endl;
return 0;
}
The second problem about storing your number in string is not difficult
if you know stringstreams in <sstream> library. stringstreams work
exactly the same as ifstream and ofstream but operate on strings not
files or standard output. They are very handy in many situations.
This is a code which I you can read and familiar with using
stringstreams and manipulators.
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
int main(int argc, char *argv[])
{
double tabd[] = {1.23456, 0.000178, 1654.8997, 10};
const size_t roz = sizeof(tabd)/sizeof(double);
cout << "Standard output:" << endl;
for(int i=0; i<roz; ++i) {
cout << "\t" << tabd[i] << endl;
}
cout << endl;
cout << "Scientific notation with lowercase e" << endl;
for(int i=0; i<roz; ++i) {
cout << scientific << setprecision(2) << "\t" << tabd[i] << endl;
}
cout << endl;
cout << "Scientific notation with uppercase E" << endl;
for(int i=0; i<roz; ++i) {
cout << scientific << setprecision(2) << uppercase << "\t" << tabd[i]
<< endl;
}
cout << endl;
ostringstream oss;
oss << scientific << setprecision(2) << uppercase << tabd[1];
cout << "iss stream contains: " << oss.str() << endl;
cout << endl;
return 0;
}