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

IO manipulators manipulate previous cout expressions?

P: n/a

The code:
// Block 1
//
cout << "Integrating " << str << " on [" << low << "," << high
<< "]" << endl << " exact answer: " << exact << endl << flush;

#if 0

// Block 2
//
for (int i = 0; i < 6; ++i )
{
cout << setw(16) << setiosflags(ios::left) << label[i] << flush;
cout << setiosflags(ios::right) << setw(20) << setiosflags(ios::fixed)
<< setprecision(17) << answer[i];
cout << " error: " << fabs(exact - answer[i]) << endl;
}

#endif
The output looks like:
Integrating x on [0,10]
exact answer: 50
However, when I delete the "#if 0" and "#endif", the output becomes:
Integrating x on [0.00000000000000000,10.00000000000000000]
exact answer: 50.00000000000000000
I tried sticking in a "flush" in Block 1 to make sure the buffer is flushed
and unaffected by the manipulators within Block 2. But it doesn't seem to do
a bit of good.

Why is it that the manipulators in block 2 affect the output generated in
block 1?

Thanks,
Pete
Apr 24 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
p@mapson.dirac.org wrote:
The code:
// Block 1
//
cout << "Integrating " << str << " on [" << low << "," << high
<< "]" << endl << " exact answer: " << exact << endl << flush;

#if 0

// Block 2
//
for (int i = 0; i < 6; ++i )
{
cout << setw(16) << setiosflags(ios::left) << label[i] << flush;
cout << setiosflags(ios::right) << setw(20) << setiosflags(ios::fixed)
<< setprecision(17) << answer[i];
cout << " error: " << fabs(exact - answer[i]) << endl;
}

#endif
The output looks like:
Integrating x on [0,10]
exact answer: 50
However, when I delete the "#if 0" and "#endif", the output becomes:
Integrating x on [0.00000000000000000,10.00000000000000000]
exact answer: 50.00000000000000000
I tried sticking in a "flush" in Block 1 to make sure the buffer is flushed
and unaffected by the manipulators within Block 2. But it doesn't seem to do
a bit of good.

Why is it that the manipulators in block 2 affect the output generated in
block 1?

Please post a minimal, compilable program which exhibits the problem.
With the short snippet that you've posted, it would seem that your loop
body is never executed... since there is no "error:" output shown.

I'm suspecting that the code snippet that you've shown is executed
multiple times, and that the first time Block 1 is executed, it works as
expected, but the 2nd and subsequent executions are showing the "odd"
behaviour.
Apr 24 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.