Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cout question

0 views
Skip to first unread message

chits

unread,
Feb 26, 2009, 7:18:18 AM2/26/09
to
Hello Friends ,

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 ?

golgepapaz

unread,
Feb 26, 2009, 7:58:07 AM2/26/09
to
use a stringstream;

std::stringstream strm;
double gh = 12354.6786545345436;
strm<< scientific <<gh;
std::cout <<strm.str();

ZikO

unread,
Feb 26, 2009, 9:45:37 AM2/26/09
to
chits wrote:
> Hello Friends ,
>
> 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)
Forget about this function it is very tricky as you flags very well
(like setting some flags needs unset the others sometimes). Have a look
at manipulators in <iomanip> library, they very convenient in use.

> 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;
}

ZikO

unread,
Feb 26, 2009, 9:56:35 AM2/26/09
to
First of all, use manipulator instead of flags() function. It's not easy
for someone who does not know flags very well. Manipulators are in
<iomanip> library. Once you've included this you are ready to use them.

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;
}

ZikO

unread,
Feb 26, 2009, 10:22:21 AM2/26/09
to
If you want to read about manipulators this is a good reference
http://www.cplusplus.com/reference/iostream/manipulators/
0 new messages