cout question

C

chits

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 ?
 
G

golgepapaz

use a stringstream;

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

ZikO

chits said:
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 said:
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 << endl;
}
cout << endl;

cout << "Scientific notation with lowercase e" << endl;
for(int i=0; i<roz; ++i) {
cout << scientific << setprecision(2) << "\t" << tabd << endl;
}
cout << endl;

cout << "Scientific notation with uppercase E" << endl;
for(int i=0; i<roz; ++i) {
cout << scientific << setprecision(2) << uppercase << "\t" << tabd
<< 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;
}
 
Z

ZikO

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

cout << "Scientific notation with lowercase e" << endl;
for(int i=0; i<roz; ++i) {
cout << scientific << setprecision(2) << "\t" << tabd << endl;
}
cout << endl;

cout << "Scientific notation with uppercase E" << endl;
for(int i=0; i<roz; ++i) {
cout << scientific << setprecision(2) << uppercase << "\t" << tabd
<< endl;
}
cout << endl;

ostringstream oss;
oss << scientific << setprecision(2) << uppercase << tabd[1];
cout << "iss stream contains: " << oss.str() << endl;
cout << endl;
return 0;
}
 

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

Similar Threads

another cout format question 4
First time question 1
cout 10
strange cout output? 13
cout formatting + copy algorithm 4
std::cout problem 4
cout vs. ofstream output 5
Little Window that Pops up for 'cout' Output 2

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,282
Latest member
RoseannaBa

Latest Threads

Top